blob: 35bc87fe00bddf32e93e1ba8144bcbd9babd64a8 [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/*
3 * Copyright (c) 2014-2018, 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
Junxiao Shi75306352018-02-01 21:59:44 +000028#include "core/fib-max-depth.hpp"
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -070029#include "core/logger.hpp"
Nick Gordon9fcf1232017-03-10 22:30:20 +000030
Junxiao Shicbc8e942016-09-06 03:17:45 +000031#include <ndn-cxx/lp/tags.hpp>
Nick Gordon9fcf1232017-03-10 22:30:20 +000032#include <ndn-cxx/mgmt/nfd/control-command.hpp>
Nick Gordon9fcf1232017-03-10 22:30:20 +000033#include <ndn-cxx/mgmt/nfd/control-parameters.hpp>
Junxiao Shi3f21e582017-05-29 15:26:32 +000034#include <ndn-cxx/mgmt/nfd/control-response.hpp>
Junxiao Shi25c6ce42016-09-09 13:49:59 +000035#include <ndn-cxx/mgmt/nfd/face-status.hpp>
36#include <ndn-cxx/mgmt/nfd/rib-entry.hpp>
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070037
38namespace nfd {
39namespace rib {
40
Davide Pesaventoa3148082018-04-12 18:21:54 -040041NFD_LOG_INIT(RibManager);
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -070042
Junxiao Shif4cfed12018-08-22 23:26:29 +000043static const std::string MGMT_MODULE_NAME = "rib";
44static const Name LOCALHOST_TOP_PREFIX = "/localhost/nfd";
Davide Pesaventoe1bdc082018-10-11 21:20:23 -040045static const time::seconds ACTIVE_FACE_FETCH_INTERVAL = 5_min;
Vince Lehman26b215c2014-08-17 15:00:41 -050046
Yanbiao Lif48d0802018-06-01 03:00:02 -070047const Name RibManager::LOCALHOP_TOP_PREFIX = "/localhop/nfd";
48
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +000049RibManager::RibManager(Rib& rib, ndn::Face& face, ndn::KeyChain& keyChain,
Davide Pesaventoe1bdc082018-10-11 21:20:23 -040050 ndn::nfd::Controller& nfdController, Dispatcher& dispatcher,
51 ndn::util::Scheduler& scheduler)
Junxiao Shifde3f542016-07-10 19:54:53 +000052 : ManagerBase(dispatcher, MGMT_MODULE_NAME)
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 Pesaventoe1bdc082018-10-11 21:20:23 -040057 , m_scheduler(scheduler)
Davide Pesavento9f8b10e2018-08-22 08:45:37 +000058 , m_faceMonitor(face)
59 , m_localhostValidator(face)
60 , m_localhopValidator(face)
Junxiao Shif4cfed12018-08-22 23:26:29 +000061 , m_isLocalhopEnabled(false)
Davide Pesaventoe1bdc082018-10-11 21:20:23 -040062 , m_activeFaceFetchEvent(m_scheduler)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070063{
Yanbiao Licf0db022016-01-29 00:54:25 -080064 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 Afanasyev3ecec502014-04-16 13:42:44 -070070}
71
Junxiao Shif4cfed12018-08-22 23:26:29 +000072void
73RibManager::applyLocalhostConfig(const ConfigSection& section, const std::string& filename)
74{
75 m_localhostValidator.load(section, filename);
76}
77
78void
79RibManager::enableLocalhop(const ConfigSection& section, const std::string& filename)
80{
81 m_localhopValidator.load(section, filename);
82 m_isLocalhopEnabled = true;
83}
84
85void
86RibManager::disableLocalhop()
87{
88 m_isLocalhopEnabled = false;
89}
Vince Lehman26b215c2014-08-17 15:00:41 -050090
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070091void
92RibManager::registerWithNfd()
93{
Junxiao Shif4cfed12018-08-22 23:26:29 +000094 registerTopPrefix(LOCALHOST_TOP_PREFIX);
Alexander Afanasyevb3893c92014-05-15 01:49:54 -070095
Junxiao Shia3295742014-05-16 22:40:10 -070096 if (m_isLocalhopEnabled) {
Junxiao Shif4cfed12018-08-22 23:26:29 +000097 registerTopPrefix(LOCALHOP_TOP_PREFIX);
Junxiao Shia3295742014-05-16 22:40:10 -070098 }
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070099
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -0700100 NFD_LOG_INFO("Start monitoring face create/destroy events");
Junxiao Shifbf78342015-01-23 14:46:41 -0700101 m_faceMonitor.onNotification.connect(bind(&RibManager::onNotification, this, _1));
Junxiao Shi15b12e72014-08-09 19:56:24 -0700102 m_faceMonitor.start();
Vince Lehmancd613c52014-07-30 14:34:49 -0500103
Vince Lehman26b215c2014-08-17 15:00:41 -0500104 scheduleActiveFaceFetch(ACTIVE_FACE_FETCH_INTERVAL);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700105}
106
107void
Eric Newberryecc45cb2016-11-08 19:57:12 +0000108RibManager::enableLocalFields()
Yanbiao Licf0db022016-01-29 00:54:25 -0800109{
Eric Newberryecc45cb2016-11-08 19:57:12 +0000110 m_nfdController.start<ndn::nfd::FaceUpdateCommand>(
Junxiao Shi52009042018-09-10 12:33:56 +0000111 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 Licf0db022016-01-29 00:54:25 -0800119}
120
121void
Junxiao Shi52009042018-09-10 12:33:56 +0000122RibManager::beginAddRoute(const Name& name, Route route, optional<time::nanoseconds> expires,
123 const std::function<void(RibUpdateResult)>& done)
Yanbiao Licf0db022016-01-29 00:54:25 -0800124{
Junxiao Shi52009042018-09-10 12:33:56 +0000125 if (expires) {
Junxiao Shi52009042018-09-10 12:33:56 +0000126 route.expires = time::steady_clock::now() + *expires;
127 }
128 else if (route.expires) {
129 expires = *route.expires - time::steady_clock::now();
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000130 }
131
132 if (expires && *expires <= 0_s) {
133 m_rib.onRouteExpiration(name, route);
134 return done(RibUpdateResult::EXPIRED);
Junxiao Shi52009042018-09-10 12:33:56 +0000135 }
136
137 NFD_LOG_INFO("Adding route " << name << " nexthop=" << route.faceId <<
138 " origin=" << route.origin << " cost=" << route.cost);
139
140 if (expires) {
Davide Pesaventoe1bdc082018-10-11 21:20:23 -0400141 auto event = m_scheduler.scheduleEvent(*expires, [=] { m_rib.onRouteExpiration(name, route); });
142 route.setExpirationEvent(event, m_scheduler);
Junxiao Shi52009042018-09-10 12:33:56 +0000143 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 Licf0db022016-01-29 00:54:25 -0800153}
154
155void
Junxiao Shi52009042018-09-10 12:33:56 +0000156RibManager::beginRemoveRoute(const Name& name, const Route& route,
157 const std::function<void(RibUpdateResult)>& done)
Yanbiao Licf0db022016-01-29 00:54:25 -0800158{
Junxiao Shi52009042018-09-10 12:33:56 +0000159 NFD_LOG_INFO("Removing route " << name << " nexthop=" << route.faceId <<
160 " origin=" << route.origin);
Yanbiao Licf0db022016-01-29 00:54:25 -0800161
Junxiao Shi52009042018-09-10 12:33:56 +0000162 RibUpdate update;
163 update.setAction(RibUpdate::UNREGISTER)
164 .setName(name)
165 .setRoute(route);
166 beginRibUpdate(update, done);
167}
168
169void
170RibManager::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 Licf0db022016-01-29 00:54:25 -0800186}
187
188void
Yanbiao Licf0db022016-01-29 00:54:25 -0800189RibManager::registerTopPrefix(const Name& topPrefix)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700190{
Junxiao Shi52009042018-09-10 12:33:56 +0000191 // add FIB nexthop
Yanbiao Licf0db022016-01-29 00:54:25 -0800192 m_nfdController.start<ndn::nfd::FibAddNextHopCommand>(
Junxiao Shi52009042018-09-10 12:33:56 +0000193 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 Afanasyev3ecec502014-04-16 13:42:44 -0700197
Junxiao Shi52009042018-09-10 12:33:56 +0000198 // 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 Shif4cfed12018-08-22 23:26:29 +0000214 m_dispatcher.addTopPrefix(topPrefix, false);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700215}
216
217void
Yanbiao Licf0db022016-01-29 00:54:25 -0800218RibManager::registerEntry(const Name& topPrefix, const Interest& interest,
219 ControlParameters parameters,
220 const ndn::mgmt::CommandContinuation& done)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700221{
Junxiao Shi75306352018-02-01 21:59:44 +0000222 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 Licf0db022016-01-29 00:54:25 -0800228 setFaceForSelfRegistration(interest, parameters);
Vince Lehman76c751c2014-11-18 17:36:38 -0600229
230 // Respond since command is valid and authorized
Yanbiao Licf0db022016-01-29 00:54:25 -0800231 done(ControlResponse(200, "Success").setBody(parameters.wireEncode()));
Alexander Afanasyevfb1c8082014-07-17 15:16:15 -0700232
Vince Lehman218be0a2015-01-15 17:25:20 -0600233 Route route;
234 route.faceId = parameters.getFaceId();
235 route.origin = parameters.getOrigin();
236 route.cost = parameters.getCost();
237 route.flags = parameters.getFlags();
Syed Obaid3313a372014-07-01 01:31:33 -0500238
Junxiao Shi52009042018-09-10 12:33:56 +0000239 optional<time::nanoseconds> expires;
Alexander Afanasyevf67cf082014-07-18 16:47:29 -0700240 if (parameters.hasExpirationPeriod() &&
Nick Gordon9fcf1232017-03-10 22:30:20 +0000241 parameters.getExpirationPeriod() != time::milliseconds::max()) {
Junxiao Shi52009042018-09-10 12:33:56 +0000242 expires = time::duration_cast<time::nanoseconds>(parameters.getExpirationPeriod());
Vince Lehman76c751c2014-11-18 17:36:38 -0600243 }
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700244
Junxiao Shi52009042018-09-10 12:33:56 +0000245 beginAddRoute(parameters.getName(), std::move(route), expires, [] (RibUpdateResult) {});
Vince Lehman281ded72014-08-21 12:17:08 -0500246}
247
248void
Yanbiao Licf0db022016-01-29 00:54:25 -0800249RibManager::unregisterEntry(const Name& topPrefix, const Interest& interest,
250 ControlParameters parameters,
251 const ndn::mgmt::CommandContinuation& done)
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700252{
Yanbiao Licf0db022016-01-29 00:54:25 -0800253 setFaceForSelfRegistration(interest, parameters);
Vince Lehman76c751c2014-11-18 17:36:38 -0600254
255 // Respond since command is valid and authorized
Yanbiao Licf0db022016-01-29 00:54:25 -0800256 done(ControlResponse(200, "Success").setBody(parameters.wireEncode()));
Alexander Afanasyevfb1c8082014-07-17 15:16:15 -0700257
Vince Lehman218be0a2015-01-15 17:25:20 -0600258 Route route;
259 route.faceId = parameters.getFaceId();
260 route.origin = parameters.getOrigin();
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700261
Junxiao Shi52009042018-09-10 12:33:56 +0000262 beginRemoveRoute(parameters.getName(), route, [] (RibUpdateResult) {});
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700263}
264
265void
Yanbiao Licf0db022016-01-29 00:54:25 -0800266RibManager::listEntries(const Name& topPrefix, const Interest& interest,
267 ndn::mgmt::StatusDatasetContext& context)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700268{
Junxiao Shi3f21e582017-05-29 15:26:32 +0000269 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 Lehman76c751c2014-11-18 17:36:38 -0600286 }
Yanbiao Licf0db022016-01-29 00:54:25 -0800287 context.end();
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700288}
289
290void
Yanbiao Licf0db022016-01-29 00:54:25 -0800291RibManager::setFaceForSelfRegistration(const Interest& request, ControlParameters& parameters)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700292{
Yanbiao Licf0db022016-01-29 00:54:25 -0800293 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 Lehman76c751c2014-11-18 17:36:38 -0600301 }
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700302}
303
Junxiao Shi21738402016-08-19 19:48:00 +0000304ndn::mgmt::Authorization
305RibManager::makeAuthorization(const std::string& verb)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700306{
Junxiao Shi21738402016-08-19 19:48:00 +0000307 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 Shif4cfed12018-08-22 23:26:29 +0000313 BOOST_ASSERT(prefix == LOCALHOST_TOP_PREFIX || prefix == LOCALHOP_TOP_PREFIX);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700314
Junxiao Shif4cfed12018-08-22 23:26:29 +0000315 ndn::ValidatorConfig& validator = prefix == LOCALHOST_TOP_PREFIX ?
Junxiao Shi21738402016-08-19 19:48:00 +0000316 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 Lehman26b215c2014-08-17 15:00:41 -0500321}
322
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000323std::ostream&
324operator<<(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
342RibManager::SlAnnounceResult
343RibManager::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
358void
359RibManager::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 Lianga4e6ec32018-10-21 09:25:00 -0700365 NFD_LOG_INFO("slAnnounce " << pa.getAnnouncedName() << " " << faceId <<
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000366 ": 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 Lianga4e6ec32018-10-21 09:25:00 -0700378 NFD_LOG_INFO("slAnnounce " << pa.getAnnouncedName() << " " << faceId << ": " << res);
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000379 cb(res);
380 });
381 },
382 [=] (const Data&, ndn::security::v2::ValidationError err) {
Teng Lianga4e6ec32018-10-21 09:25:00 -0700383 NFD_LOG_INFO("slAnnounce " << pa.getAnnouncedName() << " " << faceId <<
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000384 " validation error: " << err);
385 cb(SlAnnounceResult::VALIDATION_FAILURE);
386 }
387 );
388}
389
390void
391RibManager::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 Lianga4e6ec32018-10-21 09:25:00 -0700397 Route* oldRoute = m_rib.findLongestPrefix(name, routeQuery);
398
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000399 if (oldRoute == nullptr || !oldRoute->announcement) {
Teng Lianga4e6ec32018-10-21 09:25:00 -0700400 NFD_LOG_DEBUG("slRenew " << name << " " << faceId << ": not found");
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000401 return cb(SlAnnounceResult::NOT_FOUND);
402 }
Teng Lianga4e6ec32018-10-21 09:25:00 -0700403 Name routeName = oldRoute->announcement->getAnnouncedName();
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000404
405 Route route = *oldRoute;
406 route.expires = std::min(route.annExpires, time::steady_clock::now() + maxLifetime);
Teng Lianga4e6ec32018-10-21 09:25:00 -0700407 beginAddRoute(routeName, route, nullopt,
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000408 [=] (RibUpdateResult ribRes) {
409 auto res = getSlAnnounceResultFromRibUpdateResult(ribRes);
Teng Lianga4e6ec32018-10-21 09:25:00 -0700410 NFD_LOG_INFO("slRenew " << name << " " << faceId << ": " << res << " " << routeName);
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000411 cb(res);
412 });
413}
414
415void
416RibManager::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 Lehman26b215c2014-08-17 15:00:41 -0500435void
Vince Lehmancd613c52014-07-30 14:34:49 -0500436RibManager::fetchActiveFaces()
437{
438 NFD_LOG_DEBUG("Fetching active faces");
439
Weiwei Liu6e21cdb2016-09-29 15:16:23 -0700440 m_nfdController.fetch<ndn::nfd::FaceDataset>(
441 bind(&RibManager::removeInvalidFaces, this, _1),
442 bind(&RibManager::onFetchActiveFacesFailure, this, _1, _2),
443 ndn::nfd::CommandOptions());
Vince Lehmancd613c52014-07-30 14:34:49 -0500444}
445
446void
Weiwei Liu6e21cdb2016-09-29 15:16:23 -0700447RibManager::onFetchActiveFacesFailure(uint32_t code, const std::string& reason)
Vince Lehmancd613c52014-07-30 14:34:49 -0500448{
Weiwei Liu6e21cdb2016-09-29 15:16:23 -0700449 NFD_LOG_DEBUG("Face Status Dataset request failure " << code << " " << reason);
Yanbiao Licf0db022016-01-29 00:54:25 -0800450 scheduleActiveFaceFetch(ACTIVE_FACE_FETCH_INTERVAL);
451}
452
453void
454RibManager::onFaceDestroyedEvent(uint64_t faceId)
455{
456 m_rib.beginRemoveFace(faceId);
457 m_registeredFaces.erase(faceId);
458}
459
460void
461RibManager::scheduleActiveFaceFetch(const time::seconds& timeToWait)
462{
Davide Pesaventoe1bdc082018-10-11 21:20:23 -0400463 m_activeFaceFetchEvent = m_scheduler.scheduleEvent(timeToWait, [this] { fetchActiveFaces(); });
Yanbiao Licf0db022016-01-29 00:54:25 -0800464}
465
466void
Weiwei Liu6e21cdb2016-09-29 15:16:23 -0700467RibManager::removeInvalidFaces(const std::vector<ndn::nfd::FaceStatus>& activeFaces)
Vince Lehmancd613c52014-07-30 14:34:49 -0500468{
Vince Lehman26b215c2014-08-17 15:00:41 -0500469 NFD_LOG_DEBUG("Checking for invalid face registrations");
Vince Lehmancd613c52014-07-30 14:34:49 -0500470
Weiwei Liu6e21cdb2016-09-29 15:16:23 -0700471 FaceIdSet activeFaceIds;
Davide Pesaventod396b612017-02-20 22:11:50 -0500472 for (const auto& faceStatus : activeFaces) {
473 activeFaceIds.insert(faceStatus.getFaceId());
Junxiao Shi78926c92015-02-28 22:56:06 -0700474 }
475
Vince Lehman26b215c2014-08-17 15:00:41 -0500476 // Look for face IDs that were registered but not active to find missed
477 // face destroyed events
Davide Pesaventod396b612017-02-20 22:11:50 -0500478 for (auto faceId : m_registeredFaces) {
Weiwei Liu6e21cdb2016-09-29 15:16:23 -0700479 if (activeFaceIds.count(faceId) == 0) {
Vince Lehman76c751c2014-11-18 17:36:38 -0600480 NFD_LOG_DEBUG("Removing invalid face ID: " << faceId);
Davide Pesaventoe1bdc082018-10-11 21:20:23 -0400481 m_scheduler.scheduleEvent(0_ns, [this, faceId] { this->onFaceDestroyedEvent(faceId); });
Vince Lehman26b215c2014-08-17 15:00:41 -0500482 }
Vince Lehman76c751c2014-11-18 17:36:38 -0600483 }
Vince Lehman26b215c2014-08-17 15:00:41 -0500484
485 // Reschedule the check for future clean up
486 scheduleActiveFaceFetch(ACTIVE_FACE_FETCH_INTERVAL);
Vince Lehmancd613c52014-07-30 14:34:49 -0500487}
488
489void
Davide Pesaventod396b612017-02-20 22:11:50 -0500490RibManager::onNotification(const ndn::nfd::FaceEventNotification& notification)
Vince Lehmancd613c52014-07-30 14:34:49 -0500491{
Yanbiao Licf0db022016-01-29 00:54:25 -0800492 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 Pesaventoe1bdc082018-10-11 21:20:23 -0400496 m_scheduler.scheduleEvent(0_ns, [this, id = notification.getFaceId()] { onFaceDestroyedEvent(id); });
Yanbiao Licf0db022016-01-29 00:54:25 -0800497 }
498}
499
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700500} // namespace rib
501} // namespace nfd