blob: a6ff3df6433589e036ae78c00ad10ef9009237e5 [file] [log] [blame]
Yanbiao Li73860e32015-08-19 16:30:16 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Eric Newberry944f38b2017-07-20 20:54:22 -04002/*
Eric Newberrycb6551e2020-03-02 14:12:16 -08003 * Copyright (c) 2014-2020, Regents of the University of California,
Yanbiao Li73860e32015-08-19 16:30:16 -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.
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/>.
24 */
25
26#include "face-manager.hpp"
Davide Pesaventoa3148082018-04-12 18:21:54 -040027
Davide Pesavento2cae8ca2019-04-18 20:48:05 -040028#include "common/logger.hpp"
Junxiao Shi40cb61c2015-09-23 18:36:45 -070029#include "face/generic-link-service.hpp"
Junxiao Shib8590312016-12-29 21:22:25 +000030#include "face/protocol-factory.hpp"
Yukai Tu0a49d342015-09-13 12:54:22 +080031#include "fw/face-table.hpp"
Yanbiao Li73860e32015-08-19 16:30:16 -070032
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/channel-status.hpp>
Davide Pesavento28181322018-11-08 16:44:50 -050035#include <ndn-cxx/mgmt/nfd/face-event-notification.hpp>
36#include <ndn-cxx/mgmt/nfd/face-query-filter.hpp>
37#include <ndn-cxx/mgmt/nfd/face-status.hpp>
Yanbiao Li73860e32015-08-19 16:30:16 -070038
Yanbiao Li73860e32015-08-19 16:30:16 -070039namespace nfd {
40
Davide Pesaventoa3148082018-04-12 18:21:54 -040041NFD_LOG_INIT(FaceManager);
Yanbiao Li73860e32015-08-19 16:30:16 -070042
Junxiao Shiea47bde2017-01-26 17:49:16 +000043FaceManager::FaceManager(FaceSystem& faceSystem,
Davide Pesavento78ddcab2019-02-28 22:00:03 -050044 Dispatcher& dispatcher, CommandAuthenticator& authenticator)
45 : ManagerBase("faces", dispatcher, authenticator)
Junxiao Shiea47bde2017-01-26 17:49:16 +000046 , m_faceSystem(faceSystem)
47 , m_faceTable(faceSystem.getFaceTable())
Yanbiao Li73860e32015-08-19 16:30:16 -070048{
Yanbiao Li58ba3f92017-02-15 14:27:18 +000049 // register handlers for ControlCommand
Davide Pesavento28181322018-11-08 16:44:50 -050050 registerCommandHandler<ndn::nfd::FaceCreateCommand>("create", bind(&FaceManager::createFace, this, _4, _5));
51 registerCommandHandler<ndn::nfd::FaceUpdateCommand>("update", bind(&FaceManager::updateFace, this, _3, _4, _5));
52 registerCommandHandler<ndn::nfd::FaceDestroyCommand>("destroy", bind(&FaceManager::destroyFace, this, _4, _5));
Yanbiao Li73860e32015-08-19 16:30:16 -070053
Yanbiao Li58ba3f92017-02-15 14:27:18 +000054 // register handlers for StatusDataset
Davide Pesavento28181322018-11-08 16:44:50 -050055 registerStatusDatasetHandler("list", bind(&FaceManager::listFaces, this, _3));
56 registerStatusDatasetHandler("channels", bind(&FaceManager::listChannels, this, _3));
57 registerStatusDatasetHandler("query", bind(&FaceManager::queryFaces, this, _2, _3));
Yanbiao Li73860e32015-08-19 16:30:16 -070058
Yanbiao Li58ba3f92017-02-15 14:27:18 +000059 // register notification stream
Junxiao Shiae04d342016-07-19 13:20:22 +000060 m_postNotification = registerNotificationStream("events");
Eric Newberry1b4ba052016-10-07 23:04:07 -070061 m_faceAddConn = m_faceTable.afterAdd.connect([this] (const Face& face) {
62 connectFaceStateChangeSignal(face);
63 notifyFaceEvent(face, ndn::nfd::FACE_EVENT_CREATED);
64 });
65 m_faceRemoveConn = m_faceTable.beforeRemove.connect([this] (const Face& face) {
66 notifyFaceEvent(face, ndn::nfd::FACE_EVENT_DESTROYED);
67 });
Yanbiao Li73860e32015-08-19 16:30:16 -070068}
69
70void
Davide Pesavento28181322018-11-08 16:44:50 -050071FaceManager::createFace(const ControlParameters& parameters,
Yanbiao Li73860e32015-08-19 16:30:16 -070072 const ndn::mgmt::CommandContinuation& done)
73{
Eric Newberry78e32b02017-04-01 14:34:44 +000074 FaceUri remoteUri;
75 if (!remoteUri.parse(parameters.getUri())) {
76 NFD_LOG_TRACE("failed to parse remote URI: " << parameters.getUri());
Eric Newberry42602412016-08-27 09:33:18 -070077 done(ControlResponse(400, "Malformed command"));
78 return;
Yanbiao Li73860e32015-08-19 16:30:16 -070079 }
80
Eric Newberry78e32b02017-04-01 14:34:44 +000081 if (!remoteUri.isCanonical()) {
82 NFD_LOG_TRACE("received non-canonical remote URI: " << remoteUri.toString());
83 done(ControlResponse(400, "Non-canonical remote URI"));
Eric Newberry42602412016-08-27 09:33:18 -070084 return;
Yanbiao Li73860e32015-08-19 16:30:16 -070085 }
86
Davide Pesavento87fc0f82018-04-11 23:43:51 -040087 optional<FaceUri> localUri;
Eric Newberry78e32b02017-04-01 14:34:44 +000088 if (parameters.hasLocalUri()) {
89 localUri = FaceUri{};
90
91 if (!localUri->parse(parameters.getLocalUri())) {
92 NFD_LOG_TRACE("failed to parse local URI: " << parameters.getLocalUri());
93 done(ControlResponse(400, "Malformed command"));
94 return;
95 }
96
97 if (!localUri->isCanonical()) {
98 NFD_LOG_TRACE("received non-canonical local URI: " << localUri->toString());
99 done(ControlResponse(400, "Non-canonical local URI"));
100 return;
101 }
102 }
103
Davide Pesaventoe5eebad2017-04-06 20:23:26 -0400104 face::ProtocolFactory* factory = m_faceSystem.getFactoryByScheme(remoteUri.getScheme());
Junxiao Shib8590312016-12-29 21:22:25 +0000105 if (factory == nullptr) {
Eric Newberry78e32b02017-04-01 14:34:44 +0000106 NFD_LOG_TRACE("received create request for unsupported protocol: " << remoteUri.getScheme());
Eric Newberry42602412016-08-27 09:33:18 -0700107 done(ControlResponse(406, "Unsupported protocol"));
108 return;
Yanbiao Li73860e32015-08-19 16:30:16 -0700109 }
110
Davide Pesavento15b55052018-01-27 19:09:28 -0500111 face::FaceParams faceParams;
112 faceParams.persistency = parameters.getFacePersistency();
Eric Newberry0c3e57b2018-01-25 20:54:46 -0700113 if (parameters.hasBaseCongestionMarkingInterval()) {
114 faceParams.baseCongestionMarkingInterval = parameters.getBaseCongestionMarkingInterval();
115 }
116 if (parameters.hasDefaultCongestionThreshold()) {
117 faceParams.defaultCongestionThreshold = parameters.getDefaultCongestionThreshold();
118 }
Eric Newberry812d6152018-06-06 15:06:01 -0700119 if (parameters.hasMtu()) {
Eric Newberry13ff2592020-03-06 17:32:29 -0800120 // The face system limits MTUs to ssize_t, but the management protocol uses uint64_t
Eric Newberrycb6551e2020-03-02 14:12:16 -0800121 faceParams.mtu = std::min<uint64_t>(std::numeric_limits<ssize_t>::max(), parameters.getMtu());
Eric Newberry812d6152018-06-06 15:06:01 -0700122 }
Davide Pesavento15b55052018-01-27 19:09:28 -0500123 faceParams.wantLocalFields = parameters.hasFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED) &&
124 parameters.getFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED);
125 faceParams.wantLpReliability = parameters.hasFlagBit(ndn::nfd::BIT_LP_RELIABILITY_ENABLED) &&
126 parameters.getFlagBit(ndn::nfd::BIT_LP_RELIABILITY_ENABLED);
Eric Newberry0c3e57b2018-01-25 20:54:46 -0700127 if (parameters.hasFlagBit(ndn::nfd::BIT_CONGESTION_MARKING_ENABLED)) {
128 faceParams.wantCongestionMarking = parameters.getFlagBit(ndn::nfd::BIT_CONGESTION_MARKING_ENABLED);
129 }
Yanbiao Li73860e32015-08-19 16:30:16 -0700130 try {
Davide Pesavento15b55052018-01-27 19:09:28 -0500131 factory->createFace({remoteUri, localUri, faceParams},
Davide Pesavento28181322018-11-08 16:44:50 -0500132 [this, parameters, done] (const auto& face) {
133 this->afterCreateFaceSuccess(face, parameters, done);
134 },
135 [done] (uint32_t status, const std::string& reason) {
136 NFD_LOG_DEBUG("Face creation failed: " << reason);
137 done(ControlResponse(status, reason));
138 });
Yanbiao Li73860e32015-08-19 16:30:16 -0700139 }
140 catch (const std::runtime_error& error) {
Eric Newberry42602412016-08-27 09:33:18 -0700141 NFD_LOG_ERROR("Face creation failed: " << error.what());
142 done(ControlResponse(500, "Face creation failed due to internal error"));
143 return;
Yanbiao Li73860e32015-08-19 16:30:16 -0700144 }
145 catch (const std::logic_error& error) {
Eric Newberry42602412016-08-27 09:33:18 -0700146 NFD_LOG_ERROR("Face creation failed: " << error.what());
147 done(ControlResponse(500, "Face creation failed due to internal error"));
148 return;
Yanbiao Li73860e32015-08-19 16:30:16 -0700149 }
150}
151
Davide Pesavento28181322018-11-08 16:44:50 -0500152template<typename T>
153static void
154copyMtu(const Face& face, T& to)
155{
Eric Newberrycb6551e2020-03-02 14:12:16 -0800156 if (face.getMtu() >= 0) {
157 to.setMtu(std::min<size_t>(face.getMtu(), ndn::MAX_NDN_PACKET_SIZE));
Davide Pesavento28181322018-11-08 16:44:50 -0500158 }
Eric Newberrycb6551e2020-03-02 14:12:16 -0800159 else if (face.getMtu() == face::MTU_UNLIMITED) {
Davide Pesavento28181322018-11-08 16:44:50 -0500160 to.setMtu(ndn::MAX_NDN_PACKET_SIZE);
161 }
162}
163
164static ControlParameters
165makeUpdateFaceResponse(const Face& face)
166{
167 ControlParameters params;
168 params.setFaceId(face.getId())
169 .setFacePersistency(face.getPersistency());
Eric Newberry13ff2592020-03-06 17:32:29 -0800170 copyMtu(face, params);
Davide Pesavento28181322018-11-08 16:44:50 -0500171
172 auto linkService = dynamic_cast<face::GenericLinkService*>(face.getLinkService());
173 if (linkService != nullptr) {
174 const auto& options = linkService->getOptions();
175 params.setBaseCongestionMarkingInterval(options.baseCongestionMarkingInterval)
176 .setDefaultCongestionThreshold(options.defaultCongestionThreshold)
177 .setFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED, options.allowLocalFields, false)
178 .setFlagBit(ndn::nfd::BIT_LP_RELIABILITY_ENABLED, options.reliabilityOptions.isEnabled, false)
179 .setFlagBit(ndn::nfd::BIT_CONGESTION_MARKING_ENABLED, options.allowCongestionMarking, false);
180 }
181
182 return params;
183}
184
185static ControlParameters
186makeCreateFaceResponse(const Face& face)
187{
188 ControlParameters params = makeUpdateFaceResponse(face);
189 params.setUri(face.getRemoteUri().toString())
190 .setLocalUri(face.getLocalUri().toString());
191
Davide Pesavento28181322018-11-08 16:44:50 -0500192 return params;
193}
194
Yanbiao Li73860e32015-08-19 16:30:16 -0700195void
Davide Pesavento28181322018-11-08 16:44:50 -0500196FaceManager::afterCreateFaceSuccess(const shared_ptr<Face>& face,
197 const ControlParameters& parameters,
Yanbiao Li73860e32015-08-19 16:30:16 -0700198 const ndn::mgmt::CommandContinuation& done)
199{
Davide Pesavento28181322018-11-08 16:44:50 -0500200 if (face->getId() != face::INVALID_FACEID) { // Face already exists
Yanbiao Li58ba3f92017-02-15 14:27:18 +0000201 NFD_LOG_TRACE("Attempted to create duplicate face of " << face->getId());
Davide Pesavento28181322018-11-08 16:44:50 -0500202 ControlParameters response = makeCreateFaceResponse(*face);
Yanbiao Li58ba3f92017-02-15 14:27:18 +0000203 done(ControlResponse(409, "Face with remote URI already exists").setBody(response.wireEncode()));
204 return;
205 }
206
Eric Newberryf40551a2016-09-05 15:41:16 -0700207 // If scope non-local and flags set to enable local fields, request shouldn't
208 // have made it this far
209 BOOST_ASSERT(face->getScope() == ndn::nfd::FACE_SCOPE_LOCAL ||
210 !parameters.hasFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED) ||
211 (parameters.hasFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED) &&
212 !parameters.getFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED)));
213
214 m_faceTable.add(face);
215
Davide Pesavento28181322018-11-08 16:44:50 -0500216 ControlParameters response = makeCreateFaceResponse(*face);
Eric Newberry42602412016-08-27 09:33:18 -0700217 done(ControlResponse(200, "OK").setBody(response.wireEncode()));
218}
219
Davide Pesavento28181322018-11-08 16:44:50 -0500220static void
221updateLinkServiceOptions(Face& face, const ControlParameters& parameters)
Eric Newberry42602412016-08-27 09:33:18 -0700222{
Davide Pesavento28181322018-11-08 16:44:50 -0500223 auto linkService = dynamic_cast<face::GenericLinkService*>(face.getLinkService());
224 if (linkService == nullptr) {
225 return;
226 }
227 auto options = linkService->getOptions();
Eric Newberry42602412016-08-27 09:33:18 -0700228
Davide Pesavento28181322018-11-08 16:44:50 -0500229 if (parameters.hasFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED) &&
230 face.getScope() == ndn::nfd::FACE_SCOPE_LOCAL) {
231 options.allowLocalFields = parameters.getFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED);
232 }
233 if (parameters.hasFlagBit(ndn::nfd::BIT_LP_RELIABILITY_ENABLED)) {
234 options.reliabilityOptions.isEnabled = parameters.getFlagBit(ndn::nfd::BIT_LP_RELIABILITY_ENABLED);
235 }
236 if (parameters.hasFlagBit(ndn::nfd::BIT_CONGESTION_MARKING_ENABLED)) {
237 options.allowCongestionMarking = parameters.getFlagBit(ndn::nfd::BIT_CONGESTION_MARKING_ENABLED);
238 }
239 if (parameters.hasBaseCongestionMarkingInterval()) {
240 options.baseCongestionMarkingInterval = parameters.getBaseCongestionMarkingInterval();
241 }
242 if (parameters.hasDefaultCongestionThreshold()) {
243 options.defaultCongestionThreshold = parameters.getDefaultCongestionThreshold();
244 }
245
Eric Newberry13ff2592020-03-06 17:32:29 -0800246 if (parameters.hasMtu()) {
247 // The face system limits MTUs to ssize_t, but the management protocol uses uint64_t
248 options.overrideMtu = std::min<uint64_t>(std::numeric_limits<ssize_t>::max(), parameters.getMtu());
249 }
250
Davide Pesavento28181322018-11-08 16:44:50 -0500251 linkService->setOptions(options);
Yanbiao Li73860e32015-08-19 16:30:16 -0700252}
253
254void
Davide Pesavento28181322018-11-08 16:44:50 -0500255FaceManager::updateFace(const Interest& interest,
Eric Newberryb5aa7f52016-09-03 20:36:12 -0700256 const ControlParameters& parameters,
257 const ndn::mgmt::CommandContinuation& done)
258{
259 FaceId faceId = parameters.getFaceId();
Davide Pesavento28181322018-11-08 16:44:50 -0500260 if (faceId == 0) { // Self-update
261 auto incomingFaceIdTag = interest.getTag<lp::IncomingFaceIdTag>();
Eric Newberryb5aa7f52016-09-03 20:36:12 -0700262 if (incomingFaceIdTag == nullptr) {
263 NFD_LOG_TRACE("unable to determine face for self-update");
264 done(ControlResponse(404, "No FaceId specified and IncomingFaceId not available"));
265 return;
266 }
267 faceId = *incomingFaceIdTag;
268 }
269
270 Face* face = m_faceTable.get(faceId);
Eric Newberryb5aa7f52016-09-03 20:36:12 -0700271 if (face == nullptr) {
272 NFD_LOG_TRACE("invalid face specified");
273 done(ControlResponse(404, "Specified face does not exist"));
274 return;
275 }
276
277 // Verify validity of requested changes
278 ControlParameters response;
279 bool areParamsValid = true;
280
Eric Newberryb5aa7f52016-09-03 20:36:12 -0700281 if (parameters.hasFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED) &&
282 parameters.getFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED) &&
283 face->getScope() != ndn::nfd::FACE_SCOPE_LOCAL) {
284 NFD_LOG_TRACE("received request to enable local fields on non-local face");
285 areParamsValid = false;
286 response.setFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED,
287 parameters.getFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED));
288 }
289
Yanbiao Li58ba3f92017-02-15 14:27:18 +0000290 // check whether the requested FacePersistency change is valid if it's present
291 if (parameters.hasFacePersistency()) {
292 auto persistency = parameters.getFacePersistency();
293 if (!face->getTransport()->canChangePersistencyTo(persistency)) {
294 NFD_LOG_TRACE("cannot change face persistency to " << persistency);
295 areParamsValid = false;
296 response.setFacePersistency(persistency);
297 }
298 }
299
Eric Newberry13ff2592020-03-06 17:32:29 -0800300 // check whether the requested MTU override is valid (if it's present)
301 if (parameters.hasMtu()) {
302 auto mtu = parameters.getMtu();
303 // The face system limits MTUs to ssize_t, but the management protocol uses uint64_t
304 auto actualMtu = std::min<uint64_t>(std::numeric_limits<ssize_t>::max(), mtu);
305 auto linkService = dynamic_cast<face::GenericLinkService*>(face->getLinkService());
306 if (linkService == nullptr || !linkService->canOverrideMtuTo(actualMtu)) {
307 NFD_LOG_TRACE("cannot override face MTU to " << mtu);
308 areParamsValid = false;
309 response.setMtu(mtu);
310 }
311 }
312
Eric Newberryb5aa7f52016-09-03 20:36:12 -0700313 if (!areParamsValid) {
314 done(ControlResponse(409, "Invalid properties specified").setBody(response.wireEncode()));
315 return;
316 }
317
318 // All specified properties are valid, so make changes
Yanbiao Li58ba3f92017-02-15 14:27:18 +0000319 if (parameters.hasFacePersistency()) {
320 face->setPersistency(parameters.getFacePersistency());
321 }
Davide Pesavento28181322018-11-08 16:44:50 -0500322 updateLinkServiceOptions(*face, parameters);
Eric Newberryb5aa7f52016-09-03 20:36:12 -0700323
Davide Pesavento28181322018-11-08 16:44:50 -0500324 // Prepare and send ControlResponse
325 response = makeUpdateFaceResponse(*face);
Eric Newberryb5aa7f52016-09-03 20:36:12 -0700326 done(ControlResponse(200, "OK").setBody(response.wireEncode()));
327}
328
329void
Davide Pesavento28181322018-11-08 16:44:50 -0500330FaceManager::destroyFace(const ControlParameters& parameters,
Junxiao Shi40cb61c2015-09-23 18:36:45 -0700331 const ndn::mgmt::CommandContinuation& done)
332{
Junxiao Shi5b43f9a2016-07-19 13:15:56 +0000333 Face* face = m_faceTable.get(parameters.getFaceId());
334 if (face != nullptr) {
335 face->close();
Junxiao Shi40cb61c2015-09-23 18:36:45 -0700336 }
337
338 done(ControlResponse(200, "OK").setBody(parameters.wireEncode()));
339}
340
Davide Pesavento28181322018-11-08 16:44:50 -0500341template<typename T>
342static void
343copyFaceProperties(const Face& face, T& to)
Eric Newberryb5aa7f52016-09-03 20:36:12 -0700344{
Davide Pesavento28181322018-11-08 16:44:50 -0500345 to.setFaceId(face.getId())
346 .setRemoteUri(face.getRemoteUri().toString())
347 .setLocalUri(face.getLocalUri().toString())
348 .setFaceScope(face.getScope())
349 .setFacePersistency(face.getPersistency())
350 .setLinkType(face.getLinkType());
Eric Newberryb5aa7f52016-09-03 20:36:12 -0700351
Davide Pesavento28181322018-11-08 16:44:50 -0500352 auto linkService = dynamic_cast<face::GenericLinkService*>(face.getLinkService());
353 if (linkService != nullptr) {
354 const auto& options = linkService->getOptions();
355 to.setFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED, options.allowLocalFields)
356 .setFlagBit(ndn::nfd::BIT_LP_RELIABILITY_ENABLED, options.reliabilityOptions.isEnabled)
357 .setFlagBit(ndn::nfd::BIT_CONGESTION_MARKING_ENABLED, options.allowCongestionMarking);
Eric Newberryb5aa7f52016-09-03 20:36:12 -0700358 }
Eric Newberryb5aa7f52016-09-03 20:36:12 -0700359}
360
Davide Pesavento28181322018-11-08 16:44:50 -0500361static ndn::nfd::FaceStatus
362makeFaceStatus(const Face& face, const time::steady_clock::TimePoint& now)
Yanbiao Li58ba3f92017-02-15 14:27:18 +0000363{
Davide Pesavento28181322018-11-08 16:44:50 -0500364 ndn::nfd::FaceStatus status;
365 copyFaceProperties(face, status);
366
367 auto expirationTime = face.getExpirationTime();
368 if (expirationTime != time::steady_clock::TimePoint::max()) {
369 status.setExpirationPeriod(std::max(0_ms,
370 time::duration_cast<time::milliseconds>(expirationTime - now)));
371 }
372
Yanbiao Li58ba3f92017-02-15 14:27:18 +0000373 auto linkService = dynamic_cast<face::GenericLinkService*>(face.getLinkService());
Davide Pesavento28181322018-11-08 16:44:50 -0500374 if (linkService != nullptr) {
375 const auto& options = linkService->getOptions();
376 status.setBaseCongestionMarkingInterval(options.baseCongestionMarkingInterval)
377 .setDefaultCongestionThreshold(options.defaultCongestionThreshold);
Eric Newberry812d6152018-06-06 15:06:01 -0700378 }
379
Davide Pesavento28181322018-11-08 16:44:50 -0500380 copyMtu(face, status);
381
382 const auto& counters = face.getCounters();
383 status.setNInInterests(counters.nInInterests)
384 .setNOutInterests(counters.nOutInterests)
385 .setNInData(counters.nInData)
386 .setNOutData(counters.nOutData)
387 .setNInNacks(counters.nInNacks)
388 .setNOutNacks(counters.nOutNacks)
389 .setNInBytes(counters.nInBytes)
390 .setNOutBytes(counters.nOutBytes);
391
392 return status;
Yanbiao Li58ba3f92017-02-15 14:27:18 +0000393}
394
Eric Newberryb5aa7f52016-09-03 20:36:12 -0700395void
Davide Pesavento28181322018-11-08 16:44:50 -0500396FaceManager::listFaces(ndn::mgmt::StatusDatasetContext& context)
Yanbiao Li73860e32015-08-19 16:30:16 -0700397{
Eric Newberryc64d30a2015-12-26 11:07:27 -0700398 auto now = time::steady_clock::now();
Davide Pesavento28181322018-11-08 16:44:50 -0500399 for (const auto& face : m_faceTable) {
400 ndn::nfd::FaceStatus status = makeFaceStatus(face, now);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700401 context.append(status.wireEncode());
Yanbiao Li73860e32015-08-19 16:30:16 -0700402 }
403 context.end();
404}
405
406void
Davide Pesavento28181322018-11-08 16:44:50 -0500407FaceManager::listChannels(ndn::mgmt::StatusDatasetContext& context)
Yanbiao Li73860e32015-08-19 16:30:16 -0700408{
Davide Pesavento28181322018-11-08 16:44:50 -0500409 auto factories = m_faceSystem.listProtocolFactories();
Davide Pesaventoe5eebad2017-04-06 20:23:26 -0400410 for (const auto* factory : factories) {
Junxiao Shib8590312016-12-29 21:22:25 +0000411 for (const auto& channel : factory->getChannels()) {
412 ndn::nfd::ChannelStatus entry;
413 entry.setLocalUri(channel->getUri().toString());
414 context.append(entry.wireEncode());
Yanbiao Li73860e32015-08-19 16:30:16 -0700415 }
416 }
Yanbiao Li73860e32015-08-19 16:30:16 -0700417 context.end();
418}
419
Davide Pesavento28181322018-11-08 16:44:50 -0500420static bool
421matchFilter(const ndn::nfd::FaceQueryFilter& filter, const Face& face)
Yanbiao Li73860e32015-08-19 16:30:16 -0700422{
423 if (filter.hasFaceId() &&
Junxiao Shib84e6742016-07-19 13:16:22 +0000424 filter.getFaceId() != static_cast<uint64_t>(face.getId())) {
Yanbiao Li73860e32015-08-19 16:30:16 -0700425 return false;
426 }
427
428 if (filter.hasUriScheme() &&
Junxiao Shib84e6742016-07-19 13:16:22 +0000429 filter.getUriScheme() != face.getRemoteUri().getScheme() &&
430 filter.getUriScheme() != face.getLocalUri().getScheme()) {
Yanbiao Li73860e32015-08-19 16:30:16 -0700431 return false;
432 }
433
434 if (filter.hasRemoteUri() &&
Junxiao Shib84e6742016-07-19 13:16:22 +0000435 filter.getRemoteUri() != face.getRemoteUri().toString()) {
Yanbiao Li73860e32015-08-19 16:30:16 -0700436 return false;
437 }
438
439 if (filter.hasLocalUri() &&
Junxiao Shib84e6742016-07-19 13:16:22 +0000440 filter.getLocalUri() != face.getLocalUri().toString()) {
Yanbiao Li73860e32015-08-19 16:30:16 -0700441 return false;
442 }
443
444 if (filter.hasFaceScope() &&
Junxiao Shib84e6742016-07-19 13:16:22 +0000445 filter.getFaceScope() != face.getScope()) {
Yanbiao Li73860e32015-08-19 16:30:16 -0700446 return false;
447 }
448
449 if (filter.hasFacePersistency() &&
Junxiao Shib84e6742016-07-19 13:16:22 +0000450 filter.getFacePersistency() != face.getPersistency()) {
Yanbiao Li73860e32015-08-19 16:30:16 -0700451 return false;
452 }
453
454 if (filter.hasLinkType() &&
Junxiao Shib84e6742016-07-19 13:16:22 +0000455 filter.getLinkType() != face.getLinkType()) {
Yanbiao Li73860e32015-08-19 16:30:16 -0700456 return false;
457 }
458
459 return true;
460}
461
Junxiao Shicde37ad2015-12-24 01:02:05 -0700462void
Davide Pesavento28181322018-11-08 16:44:50 -0500463FaceManager::queryFaces(const Interest& interest,
464 ndn::mgmt::StatusDatasetContext& context)
Junxiao Shida93f1f2015-11-11 06:13:16 -0700465{
Davide Pesavento28181322018-11-08 16:44:50 -0500466 ndn::nfd::FaceQueryFilter faceFilter;
467 try {
468 faceFilter.wireDecode(interest.getName()[-1].blockFromValue());
Eric Newberry1b4ba052016-10-07 23:04:07 -0700469 }
Davide Pesavento28181322018-11-08 16:44:50 -0500470 catch (const tlv::Error& e) {
471 NFD_LOG_DEBUG("Malformed query filter: " << e.what());
472 return context.reject(ControlResponse(400, "Malformed filter"));
473 }
474
475 auto now = time::steady_clock::now();
476 for (const auto& face : m_faceTable) {
477 if (matchFilter(faceFilter, face)) {
478 ndn::nfd::FaceStatus status = makeFaceStatus(face, now);
479 context.append(status.wireEncode());
480 }
481 }
482 context.end();
Junxiao Shida93f1f2015-11-11 06:13:16 -0700483}
484
485void
Eric Newberry1b4ba052016-10-07 23:04:07 -0700486FaceManager::notifyFaceEvent(const Face& face, ndn::nfd::FaceEventKind kind)
Yanbiao Li73860e32015-08-19 16:30:16 -0700487{
488 ndn::nfd::FaceEventNotification notification;
Eric Newberry1b4ba052016-10-07 23:04:07 -0700489 notification.setKind(kind);
Davide Pesavento28181322018-11-08 16:44:50 -0500490 copyFaceProperties(face, notification);
Yanbiao Li73860e32015-08-19 16:30:16 -0700491
Junxiao Shiae04d342016-07-19 13:20:22 +0000492 m_postNotification(notification.wireEncode());
Yanbiao Li73860e32015-08-19 16:30:16 -0700493}
494
495void
Eric Newberry1b4ba052016-10-07 23:04:07 -0700496FaceManager::connectFaceStateChangeSignal(const Face& face)
Yanbiao Li73860e32015-08-19 16:30:16 -0700497{
Davide Pesavento3cf75dc2018-03-17 00:38:03 -0400498 using face::FaceState;
499
Eric Newberry1b4ba052016-10-07 23:04:07 -0700500 FaceId faceId = face.getId();
501 m_faceStateChangeConn[faceId] = face.afterStateChange.connect(
Davide Pesavento3cf75dc2018-03-17 00:38:03 -0400502 [this, faceId, &face] (FaceState oldState, FaceState newState) {
503 if (newState == FaceState::UP) {
Eric Newberry1b4ba052016-10-07 23:04:07 -0700504 notifyFaceEvent(face, ndn::nfd::FACE_EVENT_UP);
505 }
Davide Pesavento3cf75dc2018-03-17 00:38:03 -0400506 else if (newState == FaceState::DOWN) {
Eric Newberry1b4ba052016-10-07 23:04:07 -0700507 notifyFaceEvent(face, ndn::nfd::FACE_EVENT_DOWN);
508 }
Davide Pesavento3cf75dc2018-03-17 00:38:03 -0400509 else if (newState == FaceState::CLOSED) {
510 // cannot use face.getId() because it may already be reset to INVALID_FACEID
Eric Newberry1b4ba052016-10-07 23:04:07 -0700511 m_faceStateChangeConn.erase(faceId);
512 }
513 });
Yanbiao Li73860e32015-08-19 16:30:16 -0700514}
515
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200516} // namespace nfd