blob: fd5f3aa093049ec1366b91577b0ea829df1262dc [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 Newberrycb6551e2020-03-02 14:12:16 -0800120 // Cap this value at the maximum representable value in an ssize_t
121 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());
170
171 auto linkService = dynamic_cast<face::GenericLinkService*>(face.getLinkService());
172 if (linkService != nullptr) {
173 const auto& options = linkService->getOptions();
174 params.setBaseCongestionMarkingInterval(options.baseCongestionMarkingInterval)
175 .setDefaultCongestionThreshold(options.defaultCongestionThreshold)
176 .setFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED, options.allowLocalFields, false)
177 .setFlagBit(ndn::nfd::BIT_LP_RELIABILITY_ENABLED, options.reliabilityOptions.isEnabled, false)
178 .setFlagBit(ndn::nfd::BIT_CONGESTION_MARKING_ENABLED, options.allowCongestionMarking, false);
179 }
180
181 return params;
182}
183
184static ControlParameters
185makeCreateFaceResponse(const Face& face)
186{
187 ControlParameters params = makeUpdateFaceResponse(face);
188 params.setUri(face.getRemoteUri().toString())
189 .setLocalUri(face.getLocalUri().toString());
190
191 copyMtu(face, params);
192
193 return params;
194}
195
Yanbiao Li73860e32015-08-19 16:30:16 -0700196void
Davide Pesavento28181322018-11-08 16:44:50 -0500197FaceManager::afterCreateFaceSuccess(const shared_ptr<Face>& face,
198 const ControlParameters& parameters,
Yanbiao Li73860e32015-08-19 16:30:16 -0700199 const ndn::mgmt::CommandContinuation& done)
200{
Davide Pesavento28181322018-11-08 16:44:50 -0500201 if (face->getId() != face::INVALID_FACEID) { // Face already exists
Yanbiao Li58ba3f92017-02-15 14:27:18 +0000202 NFD_LOG_TRACE("Attempted to create duplicate face of " << face->getId());
Davide Pesavento28181322018-11-08 16:44:50 -0500203 ControlParameters response = makeCreateFaceResponse(*face);
Yanbiao Li58ba3f92017-02-15 14:27:18 +0000204 done(ControlResponse(409, "Face with remote URI already exists").setBody(response.wireEncode()));
205 return;
206 }
207
Eric Newberryf40551a2016-09-05 15:41:16 -0700208 // If scope non-local and flags set to enable local fields, request shouldn't
209 // have made it this far
210 BOOST_ASSERT(face->getScope() == ndn::nfd::FACE_SCOPE_LOCAL ||
211 !parameters.hasFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED) ||
212 (parameters.hasFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED) &&
213 !parameters.getFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED)));
214
215 m_faceTable.add(face);
216
Davide Pesavento28181322018-11-08 16:44:50 -0500217 ControlParameters response = makeCreateFaceResponse(*face);
Eric Newberry42602412016-08-27 09:33:18 -0700218 done(ControlResponse(200, "OK").setBody(response.wireEncode()));
219}
220
Davide Pesavento28181322018-11-08 16:44:50 -0500221static void
222updateLinkServiceOptions(Face& face, const ControlParameters& parameters)
Eric Newberry42602412016-08-27 09:33:18 -0700223{
Davide Pesavento28181322018-11-08 16:44:50 -0500224 auto linkService = dynamic_cast<face::GenericLinkService*>(face.getLinkService());
225 if (linkService == nullptr) {
226 return;
227 }
228 auto options = linkService->getOptions();
Eric Newberry42602412016-08-27 09:33:18 -0700229
Davide Pesavento28181322018-11-08 16:44:50 -0500230 if (parameters.hasFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED) &&
231 face.getScope() == ndn::nfd::FACE_SCOPE_LOCAL) {
232 options.allowLocalFields = parameters.getFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED);
233 }
234 if (parameters.hasFlagBit(ndn::nfd::BIT_LP_RELIABILITY_ENABLED)) {
235 options.reliabilityOptions.isEnabled = parameters.getFlagBit(ndn::nfd::BIT_LP_RELIABILITY_ENABLED);
236 }
237 if (parameters.hasFlagBit(ndn::nfd::BIT_CONGESTION_MARKING_ENABLED)) {
238 options.allowCongestionMarking = parameters.getFlagBit(ndn::nfd::BIT_CONGESTION_MARKING_ENABLED);
239 }
240 if (parameters.hasBaseCongestionMarkingInterval()) {
241 options.baseCongestionMarkingInterval = parameters.getBaseCongestionMarkingInterval();
242 }
243 if (parameters.hasDefaultCongestionThreshold()) {
244 options.defaultCongestionThreshold = parameters.getDefaultCongestionThreshold();
245 }
246
247 linkService->setOptions(options);
Yanbiao Li73860e32015-08-19 16:30:16 -0700248}
249
250void
Davide Pesavento28181322018-11-08 16:44:50 -0500251FaceManager::updateFace(const Interest& interest,
Eric Newberryb5aa7f52016-09-03 20:36:12 -0700252 const ControlParameters& parameters,
253 const ndn::mgmt::CommandContinuation& done)
254{
255 FaceId faceId = parameters.getFaceId();
Davide Pesavento28181322018-11-08 16:44:50 -0500256 if (faceId == 0) { // Self-update
257 auto incomingFaceIdTag = interest.getTag<lp::IncomingFaceIdTag>();
Eric Newberryb5aa7f52016-09-03 20:36:12 -0700258 if (incomingFaceIdTag == nullptr) {
259 NFD_LOG_TRACE("unable to determine face for self-update");
260 done(ControlResponse(404, "No FaceId specified and IncomingFaceId not available"));
261 return;
262 }
263 faceId = *incomingFaceIdTag;
264 }
265
266 Face* face = m_faceTable.get(faceId);
Eric Newberryb5aa7f52016-09-03 20:36:12 -0700267 if (face == nullptr) {
268 NFD_LOG_TRACE("invalid face specified");
269 done(ControlResponse(404, "Specified face does not exist"));
270 return;
271 }
272
273 // Verify validity of requested changes
274 ControlParameters response;
275 bool areParamsValid = true;
276
Eric Newberryb5aa7f52016-09-03 20:36:12 -0700277 if (parameters.hasFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED) &&
278 parameters.getFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED) &&
279 face->getScope() != ndn::nfd::FACE_SCOPE_LOCAL) {
280 NFD_LOG_TRACE("received request to enable local fields on non-local face");
281 areParamsValid = false;
282 response.setFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED,
283 parameters.getFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED));
284 }
285
Yanbiao Li58ba3f92017-02-15 14:27:18 +0000286 // check whether the requested FacePersistency change is valid if it's present
287 if (parameters.hasFacePersistency()) {
288 auto persistency = parameters.getFacePersistency();
289 if (!face->getTransport()->canChangePersistencyTo(persistency)) {
290 NFD_LOG_TRACE("cannot change face persistency to " << persistency);
291 areParamsValid = false;
292 response.setFacePersistency(persistency);
293 }
294 }
295
Eric Newberryb5aa7f52016-09-03 20:36:12 -0700296 if (!areParamsValid) {
297 done(ControlResponse(409, "Invalid properties specified").setBody(response.wireEncode()));
298 return;
299 }
300
301 // All specified properties are valid, so make changes
Yanbiao Li58ba3f92017-02-15 14:27:18 +0000302 if (parameters.hasFacePersistency()) {
303 face->setPersistency(parameters.getFacePersistency());
304 }
Davide Pesavento28181322018-11-08 16:44:50 -0500305 updateLinkServiceOptions(*face, parameters);
Eric Newberryb5aa7f52016-09-03 20:36:12 -0700306
Davide Pesavento28181322018-11-08 16:44:50 -0500307 // Prepare and send ControlResponse
308 response = makeUpdateFaceResponse(*face);
Eric Newberryb5aa7f52016-09-03 20:36:12 -0700309 done(ControlResponse(200, "OK").setBody(response.wireEncode()));
310}
311
312void
Davide Pesavento28181322018-11-08 16:44:50 -0500313FaceManager::destroyFace(const ControlParameters& parameters,
Junxiao Shi40cb61c2015-09-23 18:36:45 -0700314 const ndn::mgmt::CommandContinuation& done)
315{
Junxiao Shi5b43f9a2016-07-19 13:15:56 +0000316 Face* face = m_faceTable.get(parameters.getFaceId());
317 if (face != nullptr) {
318 face->close();
Junxiao Shi40cb61c2015-09-23 18:36:45 -0700319 }
320
321 done(ControlResponse(200, "OK").setBody(parameters.wireEncode()));
322}
323
Davide Pesavento28181322018-11-08 16:44:50 -0500324template<typename T>
325static void
326copyFaceProperties(const Face& face, T& to)
Eric Newberryb5aa7f52016-09-03 20:36:12 -0700327{
Davide Pesavento28181322018-11-08 16:44:50 -0500328 to.setFaceId(face.getId())
329 .setRemoteUri(face.getRemoteUri().toString())
330 .setLocalUri(face.getLocalUri().toString())
331 .setFaceScope(face.getScope())
332 .setFacePersistency(face.getPersistency())
333 .setLinkType(face.getLinkType());
Eric Newberryb5aa7f52016-09-03 20:36:12 -0700334
Davide Pesavento28181322018-11-08 16:44:50 -0500335 auto linkService = dynamic_cast<face::GenericLinkService*>(face.getLinkService());
336 if (linkService != nullptr) {
337 const auto& options = linkService->getOptions();
338 to.setFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED, options.allowLocalFields)
339 .setFlagBit(ndn::nfd::BIT_LP_RELIABILITY_ENABLED, options.reliabilityOptions.isEnabled)
340 .setFlagBit(ndn::nfd::BIT_CONGESTION_MARKING_ENABLED, options.allowCongestionMarking);
Eric Newberryb5aa7f52016-09-03 20:36:12 -0700341 }
Eric Newberryb5aa7f52016-09-03 20:36:12 -0700342}
343
Davide Pesavento28181322018-11-08 16:44:50 -0500344static ndn::nfd::FaceStatus
345makeFaceStatus(const Face& face, const time::steady_clock::TimePoint& now)
Yanbiao Li58ba3f92017-02-15 14:27:18 +0000346{
Davide Pesavento28181322018-11-08 16:44:50 -0500347 ndn::nfd::FaceStatus status;
348 copyFaceProperties(face, status);
349
350 auto expirationTime = face.getExpirationTime();
351 if (expirationTime != time::steady_clock::TimePoint::max()) {
352 status.setExpirationPeriod(std::max(0_ms,
353 time::duration_cast<time::milliseconds>(expirationTime - now)));
354 }
355
Yanbiao Li58ba3f92017-02-15 14:27:18 +0000356 auto linkService = dynamic_cast<face::GenericLinkService*>(face.getLinkService());
Davide Pesavento28181322018-11-08 16:44:50 -0500357 if (linkService != nullptr) {
358 const auto& options = linkService->getOptions();
359 status.setBaseCongestionMarkingInterval(options.baseCongestionMarkingInterval)
360 .setDefaultCongestionThreshold(options.defaultCongestionThreshold);
Eric Newberry812d6152018-06-06 15:06:01 -0700361 }
362
Davide Pesavento28181322018-11-08 16:44:50 -0500363 copyMtu(face, status);
364
365 const auto& counters = face.getCounters();
366 status.setNInInterests(counters.nInInterests)
367 .setNOutInterests(counters.nOutInterests)
368 .setNInData(counters.nInData)
369 .setNOutData(counters.nOutData)
370 .setNInNacks(counters.nInNacks)
371 .setNOutNacks(counters.nOutNacks)
372 .setNInBytes(counters.nInBytes)
373 .setNOutBytes(counters.nOutBytes);
374
375 return status;
Yanbiao Li58ba3f92017-02-15 14:27:18 +0000376}
377
Eric Newberryb5aa7f52016-09-03 20:36:12 -0700378void
Davide Pesavento28181322018-11-08 16:44:50 -0500379FaceManager::listFaces(ndn::mgmt::StatusDatasetContext& context)
Yanbiao Li73860e32015-08-19 16:30:16 -0700380{
Eric Newberryc64d30a2015-12-26 11:07:27 -0700381 auto now = time::steady_clock::now();
Davide Pesavento28181322018-11-08 16:44:50 -0500382 for (const auto& face : m_faceTable) {
383 ndn::nfd::FaceStatus status = makeFaceStatus(face, now);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700384 context.append(status.wireEncode());
Yanbiao Li73860e32015-08-19 16:30:16 -0700385 }
386 context.end();
387}
388
389void
Davide Pesavento28181322018-11-08 16:44:50 -0500390FaceManager::listChannels(ndn::mgmt::StatusDatasetContext& context)
Yanbiao Li73860e32015-08-19 16:30:16 -0700391{
Davide Pesavento28181322018-11-08 16:44:50 -0500392 auto factories = m_faceSystem.listProtocolFactories();
Davide Pesaventoe5eebad2017-04-06 20:23:26 -0400393 for (const auto* factory : factories) {
Junxiao Shib8590312016-12-29 21:22:25 +0000394 for (const auto& channel : factory->getChannels()) {
395 ndn::nfd::ChannelStatus entry;
396 entry.setLocalUri(channel->getUri().toString());
397 context.append(entry.wireEncode());
Yanbiao Li73860e32015-08-19 16:30:16 -0700398 }
399 }
Yanbiao Li73860e32015-08-19 16:30:16 -0700400 context.end();
401}
402
Davide Pesavento28181322018-11-08 16:44:50 -0500403static bool
404matchFilter(const ndn::nfd::FaceQueryFilter& filter, const Face& face)
Yanbiao Li73860e32015-08-19 16:30:16 -0700405{
406 if (filter.hasFaceId() &&
Junxiao Shib84e6742016-07-19 13:16:22 +0000407 filter.getFaceId() != static_cast<uint64_t>(face.getId())) {
Yanbiao Li73860e32015-08-19 16:30:16 -0700408 return false;
409 }
410
411 if (filter.hasUriScheme() &&
Junxiao Shib84e6742016-07-19 13:16:22 +0000412 filter.getUriScheme() != face.getRemoteUri().getScheme() &&
413 filter.getUriScheme() != face.getLocalUri().getScheme()) {
Yanbiao Li73860e32015-08-19 16:30:16 -0700414 return false;
415 }
416
417 if (filter.hasRemoteUri() &&
Junxiao Shib84e6742016-07-19 13:16:22 +0000418 filter.getRemoteUri() != face.getRemoteUri().toString()) {
Yanbiao Li73860e32015-08-19 16:30:16 -0700419 return false;
420 }
421
422 if (filter.hasLocalUri() &&
Junxiao Shib84e6742016-07-19 13:16:22 +0000423 filter.getLocalUri() != face.getLocalUri().toString()) {
Yanbiao Li73860e32015-08-19 16:30:16 -0700424 return false;
425 }
426
427 if (filter.hasFaceScope() &&
Junxiao Shib84e6742016-07-19 13:16:22 +0000428 filter.getFaceScope() != face.getScope()) {
Yanbiao Li73860e32015-08-19 16:30:16 -0700429 return false;
430 }
431
432 if (filter.hasFacePersistency() &&
Junxiao Shib84e6742016-07-19 13:16:22 +0000433 filter.getFacePersistency() != face.getPersistency()) {
Yanbiao Li73860e32015-08-19 16:30:16 -0700434 return false;
435 }
436
437 if (filter.hasLinkType() &&
Junxiao Shib84e6742016-07-19 13:16:22 +0000438 filter.getLinkType() != face.getLinkType()) {
Yanbiao Li73860e32015-08-19 16:30:16 -0700439 return false;
440 }
441
442 return true;
443}
444
Junxiao Shicde37ad2015-12-24 01:02:05 -0700445void
Davide Pesavento28181322018-11-08 16:44:50 -0500446FaceManager::queryFaces(const Interest& interest,
447 ndn::mgmt::StatusDatasetContext& context)
Junxiao Shida93f1f2015-11-11 06:13:16 -0700448{
Davide Pesavento28181322018-11-08 16:44:50 -0500449 ndn::nfd::FaceQueryFilter faceFilter;
450 try {
451 faceFilter.wireDecode(interest.getName()[-1].blockFromValue());
Eric Newberry1b4ba052016-10-07 23:04:07 -0700452 }
Davide Pesavento28181322018-11-08 16:44:50 -0500453 catch (const tlv::Error& e) {
454 NFD_LOG_DEBUG("Malformed query filter: " << e.what());
455 return context.reject(ControlResponse(400, "Malformed filter"));
456 }
457
458 auto now = time::steady_clock::now();
459 for (const auto& face : m_faceTable) {
460 if (matchFilter(faceFilter, face)) {
461 ndn::nfd::FaceStatus status = makeFaceStatus(face, now);
462 context.append(status.wireEncode());
463 }
464 }
465 context.end();
Junxiao Shida93f1f2015-11-11 06:13:16 -0700466}
467
468void
Eric Newberry1b4ba052016-10-07 23:04:07 -0700469FaceManager::notifyFaceEvent(const Face& face, ndn::nfd::FaceEventKind kind)
Yanbiao Li73860e32015-08-19 16:30:16 -0700470{
471 ndn::nfd::FaceEventNotification notification;
Eric Newberry1b4ba052016-10-07 23:04:07 -0700472 notification.setKind(kind);
Davide Pesavento28181322018-11-08 16:44:50 -0500473 copyFaceProperties(face, notification);
Yanbiao Li73860e32015-08-19 16:30:16 -0700474
Junxiao Shiae04d342016-07-19 13:20:22 +0000475 m_postNotification(notification.wireEncode());
Yanbiao Li73860e32015-08-19 16:30:16 -0700476}
477
478void
Eric Newberry1b4ba052016-10-07 23:04:07 -0700479FaceManager::connectFaceStateChangeSignal(const Face& face)
Yanbiao Li73860e32015-08-19 16:30:16 -0700480{
Davide Pesavento3cf75dc2018-03-17 00:38:03 -0400481 using face::FaceState;
482
Eric Newberry1b4ba052016-10-07 23:04:07 -0700483 FaceId faceId = face.getId();
484 m_faceStateChangeConn[faceId] = face.afterStateChange.connect(
Davide Pesavento3cf75dc2018-03-17 00:38:03 -0400485 [this, faceId, &face] (FaceState oldState, FaceState newState) {
486 if (newState == FaceState::UP) {
Eric Newberry1b4ba052016-10-07 23:04:07 -0700487 notifyFaceEvent(face, ndn::nfd::FACE_EVENT_UP);
488 }
Davide Pesavento3cf75dc2018-03-17 00:38:03 -0400489 else if (newState == FaceState::DOWN) {
Eric Newberry1b4ba052016-10-07 23:04:07 -0700490 notifyFaceEvent(face, ndn::nfd::FACE_EVENT_DOWN);
491 }
Davide Pesavento3cf75dc2018-03-17 00:38:03 -0400492 else if (newState == FaceState::CLOSED) {
493 // cannot use face.getId() because it may already be reset to INVALID_FACEID
Eric Newberry1b4ba052016-10-07 23:04:07 -0700494 m_faceStateChangeConn.erase(faceId);
495 }
496 });
Yanbiao Li73860e32015-08-19 16:30:16 -0700497}
498
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200499} // namespace nfd