blob: b7109fac51632ab025fcaae355f183f289aa5f38 [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/*
Davide Pesaventoae430302023-05-11 01:42:46 -04003 * Copyright (c) 2014-2023, 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 Pesavento412c9822021-07-02 00:21:05 -040050 registerCommandHandler<ndn::nfd::FaceCreateCommand>("create",
Davide Pesaventoae430302023-05-11 01:42:46 -040051 [this] (auto&&, auto&&, auto&&, auto&&... args) { createFace(std::forward<decltype(args)>(args)...); });
Davide Pesavento412c9822021-07-02 00:21:05 -040052 registerCommandHandler<ndn::nfd::FaceUpdateCommand>("update",
Davide Pesaventoae430302023-05-11 01:42:46 -040053 [this] (auto&&, auto&&, auto&&... args) { updateFace(std::forward<decltype(args)>(args)...); });
Davide Pesavento412c9822021-07-02 00:21:05 -040054 registerCommandHandler<ndn::nfd::FaceDestroyCommand>("destroy",
Davide Pesaventoae430302023-05-11 01:42:46 -040055 [this] (auto&&, auto&&, auto&&, auto&&... args) { destroyFace(std::forward<decltype(args)>(args)...); });
Yanbiao Li73860e32015-08-19 16:30:16 -070056
Yanbiao Li58ba3f92017-02-15 14:27:18 +000057 // register handlers for StatusDataset
Davide Pesaventoae430302023-05-11 01:42:46 -040058 registerStatusDatasetHandler("list",
59 [this] (auto&&, auto&&, auto&&... args) { listFaces(std::forward<decltype(args)>(args)...); });
60 registerStatusDatasetHandler("channels",
61 [this] (auto&&, auto&&, auto&&... args) { listChannels(std::forward<decltype(args)>(args)...); });
62 registerStatusDatasetHandler("query",
63 [this] (auto&&, auto&&... args) { queryFaces(std::forward<decltype(args)>(args)...); });
Yanbiao Li73860e32015-08-19 16:30:16 -070064
Yanbiao Li58ba3f92017-02-15 14:27:18 +000065 // register notification stream
Junxiao Shiae04d342016-07-19 13:20:22 +000066 m_postNotification = registerNotificationStream("events");
Eric Newberry1b4ba052016-10-07 23:04:07 -070067 m_faceAddConn = m_faceTable.afterAdd.connect([this] (const Face& face) {
68 connectFaceStateChangeSignal(face);
69 notifyFaceEvent(face, ndn::nfd::FACE_EVENT_CREATED);
70 });
71 m_faceRemoveConn = m_faceTable.beforeRemove.connect([this] (const Face& face) {
72 notifyFaceEvent(face, ndn::nfd::FACE_EVENT_DESTROYED);
73 });
Yanbiao Li73860e32015-08-19 16:30:16 -070074}
75
76void
Davide Pesavento28181322018-11-08 16:44:50 -050077FaceManager::createFace(const ControlParameters& parameters,
Yanbiao Li73860e32015-08-19 16:30:16 -070078 const ndn::mgmt::CommandContinuation& done)
79{
Eric Newberry78e32b02017-04-01 14:34:44 +000080 FaceUri remoteUri;
81 if (!remoteUri.parse(parameters.getUri())) {
82 NFD_LOG_TRACE("failed to parse remote URI: " << parameters.getUri());
Eric Newberry42602412016-08-27 09:33:18 -070083 done(ControlResponse(400, "Malformed command"));
84 return;
Yanbiao Li73860e32015-08-19 16:30:16 -070085 }
86
Eric Newberry78e32b02017-04-01 14:34:44 +000087 if (!remoteUri.isCanonical()) {
88 NFD_LOG_TRACE("received non-canonical remote URI: " << remoteUri.toString());
89 done(ControlResponse(400, "Non-canonical remote URI"));
Eric Newberry42602412016-08-27 09:33:18 -070090 return;
Yanbiao Li73860e32015-08-19 16:30:16 -070091 }
92
Davide Pesaventob7bfcb92022-05-22 23:55:23 -040093 std::optional<FaceUri> localUri;
Eric Newberry78e32b02017-04-01 14:34:44 +000094 if (parameters.hasLocalUri()) {
95 localUri = FaceUri{};
96
97 if (!localUri->parse(parameters.getLocalUri())) {
98 NFD_LOG_TRACE("failed to parse local URI: " << parameters.getLocalUri());
99 done(ControlResponse(400, "Malformed command"));
100 return;
101 }
102
103 if (!localUri->isCanonical()) {
104 NFD_LOG_TRACE("received non-canonical local URI: " << localUri->toString());
105 done(ControlResponse(400, "Non-canonical local URI"));
106 return;
107 }
108 }
109
Davide Pesaventoe5eebad2017-04-06 20:23:26 -0400110 face::ProtocolFactory* factory = m_faceSystem.getFactoryByScheme(remoteUri.getScheme());
Junxiao Shib8590312016-12-29 21:22:25 +0000111 if (factory == nullptr) {
Eric Newberry78e32b02017-04-01 14:34:44 +0000112 NFD_LOG_TRACE("received create request for unsupported protocol: " << remoteUri.getScheme());
Eric Newberry42602412016-08-27 09:33:18 -0700113 done(ControlResponse(406, "Unsupported protocol"));
114 return;
Yanbiao Li73860e32015-08-19 16:30:16 -0700115 }
116
Davide Pesavento15b55052018-01-27 19:09:28 -0500117 face::FaceParams faceParams;
118 faceParams.persistency = parameters.getFacePersistency();
Eric Newberry0c3e57b2018-01-25 20:54:46 -0700119 if (parameters.hasBaseCongestionMarkingInterval()) {
120 faceParams.baseCongestionMarkingInterval = parameters.getBaseCongestionMarkingInterval();
121 }
122 if (parameters.hasDefaultCongestionThreshold()) {
123 faceParams.defaultCongestionThreshold = parameters.getDefaultCongestionThreshold();
124 }
Eric Newberry812d6152018-06-06 15:06:01 -0700125 if (parameters.hasMtu()) {
Eric Newberry13ff2592020-03-06 17:32:29 -0800126 // The face system limits MTUs to ssize_t, but the management protocol uses uint64_t
Eric Newberrycb6551e2020-03-02 14:12:16 -0800127 faceParams.mtu = std::min<uint64_t>(std::numeric_limits<ssize_t>::max(), parameters.getMtu());
Eric Newberry812d6152018-06-06 15:06:01 -0700128 }
Davide Pesavento15b55052018-01-27 19:09:28 -0500129 faceParams.wantLocalFields = parameters.hasFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED) &&
130 parameters.getFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED);
131 faceParams.wantLpReliability = parameters.hasFlagBit(ndn::nfd::BIT_LP_RELIABILITY_ENABLED) &&
132 parameters.getFlagBit(ndn::nfd::BIT_LP_RELIABILITY_ENABLED);
Eric Newberry0c3e57b2018-01-25 20:54:46 -0700133 if (parameters.hasFlagBit(ndn::nfd::BIT_CONGESTION_MARKING_ENABLED)) {
134 faceParams.wantCongestionMarking = parameters.getFlagBit(ndn::nfd::BIT_CONGESTION_MARKING_ENABLED);
135 }
Yanbiao Li73860e32015-08-19 16:30:16 -0700136 try {
Davide Pesavento15b55052018-01-27 19:09:28 -0500137 factory->createFace({remoteUri, localUri, faceParams},
Davide Pesavento28181322018-11-08 16:44:50 -0500138 [this, parameters, done] (const auto& face) {
139 this->afterCreateFaceSuccess(face, parameters, done);
140 },
141 [done] (uint32_t status, const std::string& reason) {
142 NFD_LOG_DEBUG("Face creation failed: " << reason);
143 done(ControlResponse(status, reason));
144 });
Yanbiao Li73860e32015-08-19 16:30:16 -0700145 }
146 catch (const std::runtime_error& error) {
Eric Newberry42602412016-08-27 09:33:18 -0700147 NFD_LOG_ERROR("Face creation failed: " << error.what());
148 done(ControlResponse(500, "Face creation failed due to internal error"));
149 return;
Yanbiao Li73860e32015-08-19 16:30:16 -0700150 }
151 catch (const std::logic_error& error) {
Eric Newberry42602412016-08-27 09:33:18 -0700152 NFD_LOG_ERROR("Face creation failed: " << error.what());
153 done(ControlResponse(500, "Face creation failed due to internal error"));
154 return;
Yanbiao Li73860e32015-08-19 16:30:16 -0700155 }
156}
157
Davide Pesavento28181322018-11-08 16:44:50 -0500158template<typename T>
159static void
160copyMtu(const Face& face, T& to)
161{
Eric Newberrycb6551e2020-03-02 14:12:16 -0800162 if (face.getMtu() >= 0) {
163 to.setMtu(std::min<size_t>(face.getMtu(), ndn::MAX_NDN_PACKET_SIZE));
Davide Pesavento28181322018-11-08 16:44:50 -0500164 }
Eric Newberrycb6551e2020-03-02 14:12:16 -0800165 else if (face.getMtu() == face::MTU_UNLIMITED) {
Davide Pesavento28181322018-11-08 16:44:50 -0500166 to.setMtu(ndn::MAX_NDN_PACKET_SIZE);
167 }
168}
169
170static ControlParameters
171makeUpdateFaceResponse(const Face& face)
172{
173 ControlParameters params;
174 params.setFaceId(face.getId())
175 .setFacePersistency(face.getPersistency());
Eric Newberry13ff2592020-03-06 17:32:29 -0800176 copyMtu(face, params);
Davide Pesavento28181322018-11-08 16:44:50 -0500177
178 auto linkService = dynamic_cast<face::GenericLinkService*>(face.getLinkService());
179 if (linkService != nullptr) {
180 const auto& options = linkService->getOptions();
181 params.setBaseCongestionMarkingInterval(options.baseCongestionMarkingInterval)
182 .setDefaultCongestionThreshold(options.defaultCongestionThreshold)
183 .setFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED, options.allowLocalFields, false)
184 .setFlagBit(ndn::nfd::BIT_LP_RELIABILITY_ENABLED, options.reliabilityOptions.isEnabled, false)
185 .setFlagBit(ndn::nfd::BIT_CONGESTION_MARKING_ENABLED, options.allowCongestionMarking, false);
186 }
187
188 return params;
189}
190
191static ControlParameters
192makeCreateFaceResponse(const Face& face)
193{
194 ControlParameters params = makeUpdateFaceResponse(face);
195 params.setUri(face.getRemoteUri().toString())
196 .setLocalUri(face.getLocalUri().toString());
197
Davide Pesavento28181322018-11-08 16:44:50 -0500198 return params;
199}
200
Yanbiao Li73860e32015-08-19 16:30:16 -0700201void
Davide Pesavento28181322018-11-08 16:44:50 -0500202FaceManager::afterCreateFaceSuccess(const shared_ptr<Face>& face,
203 const ControlParameters& parameters,
Yanbiao Li73860e32015-08-19 16:30:16 -0700204 const ndn::mgmt::CommandContinuation& done)
205{
Davide Pesavento28181322018-11-08 16:44:50 -0500206 if (face->getId() != face::INVALID_FACEID) { // Face already exists
Yanbiao Li58ba3f92017-02-15 14:27:18 +0000207 NFD_LOG_TRACE("Attempted to create duplicate face of " << face->getId());
Davide Pesavento28181322018-11-08 16:44:50 -0500208 ControlParameters response = makeCreateFaceResponse(*face);
Yanbiao Li58ba3f92017-02-15 14:27:18 +0000209 done(ControlResponse(409, "Face with remote URI already exists").setBody(response.wireEncode()));
210 return;
211 }
212
Eric Newberryf40551a2016-09-05 15:41:16 -0700213 // If scope non-local and flags set to enable local fields, request shouldn't
214 // have made it this far
215 BOOST_ASSERT(face->getScope() == ndn::nfd::FACE_SCOPE_LOCAL ||
216 !parameters.hasFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED) ||
217 (parameters.hasFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED) &&
218 !parameters.getFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED)));
219
220 m_faceTable.add(face);
221
Davide Pesavento28181322018-11-08 16:44:50 -0500222 ControlParameters response = makeCreateFaceResponse(*face);
Eric Newberry42602412016-08-27 09:33:18 -0700223 done(ControlResponse(200, "OK").setBody(response.wireEncode()));
224}
225
Davide Pesavento28181322018-11-08 16:44:50 -0500226static void
227updateLinkServiceOptions(Face& face, const ControlParameters& parameters)
Eric Newberry42602412016-08-27 09:33:18 -0700228{
Davide Pesavento28181322018-11-08 16:44:50 -0500229 auto linkService = dynamic_cast<face::GenericLinkService*>(face.getLinkService());
230 if (linkService == nullptr) {
231 return;
232 }
233 auto options = linkService->getOptions();
Eric Newberry42602412016-08-27 09:33:18 -0700234
Davide Pesavento28181322018-11-08 16:44:50 -0500235 if (parameters.hasFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED) &&
236 face.getScope() == ndn::nfd::FACE_SCOPE_LOCAL) {
237 options.allowLocalFields = parameters.getFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED);
238 }
239 if (parameters.hasFlagBit(ndn::nfd::BIT_LP_RELIABILITY_ENABLED)) {
240 options.reliabilityOptions.isEnabled = parameters.getFlagBit(ndn::nfd::BIT_LP_RELIABILITY_ENABLED);
241 }
242 if (parameters.hasFlagBit(ndn::nfd::BIT_CONGESTION_MARKING_ENABLED)) {
243 options.allowCongestionMarking = parameters.getFlagBit(ndn::nfd::BIT_CONGESTION_MARKING_ENABLED);
244 }
245 if (parameters.hasBaseCongestionMarkingInterval()) {
246 options.baseCongestionMarkingInterval = parameters.getBaseCongestionMarkingInterval();
247 }
248 if (parameters.hasDefaultCongestionThreshold()) {
249 options.defaultCongestionThreshold = parameters.getDefaultCongestionThreshold();
250 }
251
Eric Newberry13ff2592020-03-06 17:32:29 -0800252 if (parameters.hasMtu()) {
253 // The face system limits MTUs to ssize_t, but the management protocol uses uint64_t
254 options.overrideMtu = std::min<uint64_t>(std::numeric_limits<ssize_t>::max(), parameters.getMtu());
255 }
256
Davide Pesavento28181322018-11-08 16:44:50 -0500257 linkService->setOptions(options);
Yanbiao Li73860e32015-08-19 16:30:16 -0700258}
259
260void
Davide Pesavento28181322018-11-08 16:44:50 -0500261FaceManager::updateFace(const Interest& interest,
Eric Newberryb5aa7f52016-09-03 20:36:12 -0700262 const ControlParameters& parameters,
263 const ndn::mgmt::CommandContinuation& done)
264{
265 FaceId faceId = parameters.getFaceId();
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400266 if (faceId == face::INVALID_FACEID) { // Self-update
Davide Pesavento28181322018-11-08 16:44:50 -0500267 auto incomingFaceIdTag = interest.getTag<lp::IncomingFaceIdTag>();
Eric Newberryb5aa7f52016-09-03 20:36:12 -0700268 if (incomingFaceIdTag == nullptr) {
269 NFD_LOG_TRACE("unable to determine face for self-update");
270 done(ControlResponse(404, "No FaceId specified and IncomingFaceId not available"));
271 return;
272 }
273 faceId = *incomingFaceIdTag;
274 }
275
276 Face* face = m_faceTable.get(faceId);
Eric Newberryb5aa7f52016-09-03 20:36:12 -0700277 if (face == nullptr) {
278 NFD_LOG_TRACE("invalid face specified");
279 done(ControlResponse(404, "Specified face does not exist"));
280 return;
281 }
282
283 // Verify validity of requested changes
284 ControlParameters response;
285 bool areParamsValid = true;
286
Eric Newberryb5aa7f52016-09-03 20:36:12 -0700287 if (parameters.hasFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED) &&
288 parameters.getFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED) &&
289 face->getScope() != ndn::nfd::FACE_SCOPE_LOCAL) {
290 NFD_LOG_TRACE("received request to enable local fields on non-local face");
291 areParamsValid = false;
292 response.setFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED,
293 parameters.getFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED));
294 }
295
Yanbiao Li58ba3f92017-02-15 14:27:18 +0000296 // check whether the requested FacePersistency change is valid if it's present
297 if (parameters.hasFacePersistency()) {
298 auto persistency = parameters.getFacePersistency();
299 if (!face->getTransport()->canChangePersistencyTo(persistency)) {
300 NFD_LOG_TRACE("cannot change face persistency to " << persistency);
301 areParamsValid = false;
302 response.setFacePersistency(persistency);
303 }
304 }
305
Eric Newberry13ff2592020-03-06 17:32:29 -0800306 // check whether the requested MTU override is valid (if it's present)
307 if (parameters.hasMtu()) {
308 auto mtu = parameters.getMtu();
309 // The face system limits MTUs to ssize_t, but the management protocol uses uint64_t
310 auto actualMtu = std::min<uint64_t>(std::numeric_limits<ssize_t>::max(), mtu);
311 auto linkService = dynamic_cast<face::GenericLinkService*>(face->getLinkService());
312 if (linkService == nullptr || !linkService->canOverrideMtuTo(actualMtu)) {
313 NFD_LOG_TRACE("cannot override face MTU to " << mtu);
314 areParamsValid = false;
315 response.setMtu(mtu);
316 }
317 }
318
Eric Newberryb5aa7f52016-09-03 20:36:12 -0700319 if (!areParamsValid) {
320 done(ControlResponse(409, "Invalid properties specified").setBody(response.wireEncode()));
321 return;
322 }
323
324 // All specified properties are valid, so make changes
Yanbiao Li58ba3f92017-02-15 14:27:18 +0000325 if (parameters.hasFacePersistency()) {
326 face->setPersistency(parameters.getFacePersistency());
327 }
Davide Pesavento28181322018-11-08 16:44:50 -0500328 updateLinkServiceOptions(*face, parameters);
Eric Newberryb5aa7f52016-09-03 20:36:12 -0700329
Davide Pesavento28181322018-11-08 16:44:50 -0500330 // Prepare and send ControlResponse
331 response = makeUpdateFaceResponse(*face);
Eric Newberryb5aa7f52016-09-03 20:36:12 -0700332 done(ControlResponse(200, "OK").setBody(response.wireEncode()));
333}
334
335void
Davide Pesavento28181322018-11-08 16:44:50 -0500336FaceManager::destroyFace(const ControlParameters& parameters,
Junxiao Shi40cb61c2015-09-23 18:36:45 -0700337 const ndn::mgmt::CommandContinuation& done)
338{
Junxiao Shi5b43f9a2016-07-19 13:15:56 +0000339 Face* face = m_faceTable.get(parameters.getFaceId());
340 if (face != nullptr) {
341 face->close();
Junxiao Shi40cb61c2015-09-23 18:36:45 -0700342 }
343
344 done(ControlResponse(200, "OK").setBody(parameters.wireEncode()));
345}
346
Davide Pesavento28181322018-11-08 16:44:50 -0500347template<typename T>
348static void
349copyFaceProperties(const Face& face, T& to)
Eric Newberryb5aa7f52016-09-03 20:36:12 -0700350{
Davide Pesavento28181322018-11-08 16:44:50 -0500351 to.setFaceId(face.getId())
352 .setRemoteUri(face.getRemoteUri().toString())
353 .setLocalUri(face.getLocalUri().toString())
354 .setFaceScope(face.getScope())
355 .setFacePersistency(face.getPersistency())
356 .setLinkType(face.getLinkType());
Eric Newberryb5aa7f52016-09-03 20:36:12 -0700357
Davide Pesavento28181322018-11-08 16:44:50 -0500358 auto linkService = dynamic_cast<face::GenericLinkService*>(face.getLinkService());
359 if (linkService != nullptr) {
360 const auto& options = linkService->getOptions();
361 to.setFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED, options.allowLocalFields)
362 .setFlagBit(ndn::nfd::BIT_LP_RELIABILITY_ENABLED, options.reliabilityOptions.isEnabled)
363 .setFlagBit(ndn::nfd::BIT_CONGESTION_MARKING_ENABLED, options.allowCongestionMarking);
Eric Newberryb5aa7f52016-09-03 20:36:12 -0700364 }
Eric Newberryb5aa7f52016-09-03 20:36:12 -0700365}
366
Davide Pesavento28181322018-11-08 16:44:50 -0500367static ndn::nfd::FaceStatus
Davide Pesavento412c9822021-07-02 00:21:05 -0400368makeFaceStatus(const Face& face, const time::steady_clock::time_point& now)
Yanbiao Li58ba3f92017-02-15 14:27:18 +0000369{
Davide Pesavento28181322018-11-08 16:44:50 -0500370 ndn::nfd::FaceStatus status;
371 copyFaceProperties(face, status);
372
373 auto expirationTime = face.getExpirationTime();
Davide Pesavento412c9822021-07-02 00:21:05 -0400374 if (expirationTime != time::steady_clock::time_point::max()) {
Davide Pesavento28181322018-11-08 16:44:50 -0500375 status.setExpirationPeriod(std::max(0_ms,
376 time::duration_cast<time::milliseconds>(expirationTime - now)));
377 }
378
Yanbiao Li58ba3f92017-02-15 14:27:18 +0000379 auto linkService = dynamic_cast<face::GenericLinkService*>(face.getLinkService());
Davide Pesavento28181322018-11-08 16:44:50 -0500380 if (linkService != nullptr) {
381 const auto& options = linkService->getOptions();
382 status.setBaseCongestionMarkingInterval(options.baseCongestionMarkingInterval)
383 .setDefaultCongestionThreshold(options.defaultCongestionThreshold);
Eric Newberry812d6152018-06-06 15:06:01 -0700384 }
385
Davide Pesavento28181322018-11-08 16:44:50 -0500386 copyMtu(face, status);
387
388 const auto& counters = face.getCounters();
389 status.setNInInterests(counters.nInInterests)
390 .setNOutInterests(counters.nOutInterests)
391 .setNInData(counters.nInData)
392 .setNOutData(counters.nOutData)
393 .setNInNacks(counters.nInNacks)
394 .setNOutNacks(counters.nOutNacks)
395 .setNInBytes(counters.nInBytes)
396 .setNOutBytes(counters.nOutBytes);
397
398 return status;
Yanbiao Li58ba3f92017-02-15 14:27:18 +0000399}
400
Eric Newberryb5aa7f52016-09-03 20:36:12 -0700401void
Davide Pesavento28181322018-11-08 16:44:50 -0500402FaceManager::listFaces(ndn::mgmt::StatusDatasetContext& context)
Yanbiao Li73860e32015-08-19 16:30:16 -0700403{
Eric Newberryc64d30a2015-12-26 11:07:27 -0700404 auto now = time::steady_clock::now();
Davide Pesavento28181322018-11-08 16:44:50 -0500405 for (const auto& face : m_faceTable) {
406 ndn::nfd::FaceStatus status = makeFaceStatus(face, now);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700407 context.append(status.wireEncode());
Yanbiao Li73860e32015-08-19 16:30:16 -0700408 }
409 context.end();
410}
411
412void
Davide Pesavento28181322018-11-08 16:44:50 -0500413FaceManager::listChannels(ndn::mgmt::StatusDatasetContext& context)
Yanbiao Li73860e32015-08-19 16:30:16 -0700414{
Davide Pesavento28181322018-11-08 16:44:50 -0500415 auto factories = m_faceSystem.listProtocolFactories();
Davide Pesaventoe5eebad2017-04-06 20:23:26 -0400416 for (const auto* factory : factories) {
Junxiao Shib8590312016-12-29 21:22:25 +0000417 for (const auto& channel : factory->getChannels()) {
418 ndn::nfd::ChannelStatus entry;
419 entry.setLocalUri(channel->getUri().toString());
420 context.append(entry.wireEncode());
Yanbiao Li73860e32015-08-19 16:30:16 -0700421 }
422 }
Yanbiao Li73860e32015-08-19 16:30:16 -0700423 context.end();
424}
425
Davide Pesavento28181322018-11-08 16:44:50 -0500426static bool
427matchFilter(const ndn::nfd::FaceQueryFilter& filter, const Face& face)
Yanbiao Li73860e32015-08-19 16:30:16 -0700428{
429 if (filter.hasFaceId() &&
Junxiao Shib84e6742016-07-19 13:16:22 +0000430 filter.getFaceId() != static_cast<uint64_t>(face.getId())) {
Yanbiao Li73860e32015-08-19 16:30:16 -0700431 return false;
432 }
433
434 if (filter.hasUriScheme() &&
Junxiao Shib84e6742016-07-19 13:16:22 +0000435 filter.getUriScheme() != face.getRemoteUri().getScheme() &&
436 filter.getUriScheme() != face.getLocalUri().getScheme()) {
Yanbiao Li73860e32015-08-19 16:30:16 -0700437 return false;
438 }
439
440 if (filter.hasRemoteUri() &&
Junxiao Shib84e6742016-07-19 13:16:22 +0000441 filter.getRemoteUri() != face.getRemoteUri().toString()) {
Yanbiao Li73860e32015-08-19 16:30:16 -0700442 return false;
443 }
444
445 if (filter.hasLocalUri() &&
Junxiao Shib84e6742016-07-19 13:16:22 +0000446 filter.getLocalUri() != face.getLocalUri().toString()) {
Yanbiao Li73860e32015-08-19 16:30:16 -0700447 return false;
448 }
449
450 if (filter.hasFaceScope() &&
Junxiao Shib84e6742016-07-19 13:16:22 +0000451 filter.getFaceScope() != face.getScope()) {
Yanbiao Li73860e32015-08-19 16:30:16 -0700452 return false;
453 }
454
455 if (filter.hasFacePersistency() &&
Junxiao Shib84e6742016-07-19 13:16:22 +0000456 filter.getFacePersistency() != face.getPersistency()) {
Yanbiao Li73860e32015-08-19 16:30:16 -0700457 return false;
458 }
459
460 if (filter.hasLinkType() &&
Junxiao Shib84e6742016-07-19 13:16:22 +0000461 filter.getLinkType() != face.getLinkType()) {
Yanbiao Li73860e32015-08-19 16:30:16 -0700462 return false;
463 }
464
465 return true;
466}
467
Junxiao Shicde37ad2015-12-24 01:02:05 -0700468void
Davide Pesavento28181322018-11-08 16:44:50 -0500469FaceManager::queryFaces(const Interest& interest,
470 ndn::mgmt::StatusDatasetContext& context)
Junxiao Shida93f1f2015-11-11 06:13:16 -0700471{
Davide Pesavento28181322018-11-08 16:44:50 -0500472 ndn::nfd::FaceQueryFilter faceFilter;
473 try {
474 faceFilter.wireDecode(interest.getName()[-1].blockFromValue());
Eric Newberry1b4ba052016-10-07 23:04:07 -0700475 }
Davide Pesavento28181322018-11-08 16:44:50 -0500476 catch (const tlv::Error& e) {
477 NFD_LOG_DEBUG("Malformed query filter: " << e.what());
478 return context.reject(ControlResponse(400, "Malformed filter"));
479 }
480
481 auto now = time::steady_clock::now();
482 for (const auto& face : m_faceTable) {
483 if (matchFilter(faceFilter, face)) {
484 ndn::nfd::FaceStatus status = makeFaceStatus(face, now);
485 context.append(status.wireEncode());
486 }
487 }
488 context.end();
Junxiao Shida93f1f2015-11-11 06:13:16 -0700489}
490
491void
Eric Newberry1b4ba052016-10-07 23:04:07 -0700492FaceManager::notifyFaceEvent(const Face& face, ndn::nfd::FaceEventKind kind)
Yanbiao Li73860e32015-08-19 16:30:16 -0700493{
494 ndn::nfd::FaceEventNotification notification;
Eric Newberry1b4ba052016-10-07 23:04:07 -0700495 notification.setKind(kind);
Davide Pesavento28181322018-11-08 16:44:50 -0500496 copyFaceProperties(face, notification);
Yanbiao Li73860e32015-08-19 16:30:16 -0700497
Junxiao Shiae04d342016-07-19 13:20:22 +0000498 m_postNotification(notification.wireEncode());
Yanbiao Li73860e32015-08-19 16:30:16 -0700499}
500
501void
Eric Newberry1b4ba052016-10-07 23:04:07 -0700502FaceManager::connectFaceStateChangeSignal(const Face& face)
Yanbiao Li73860e32015-08-19 16:30:16 -0700503{
Davide Pesavento3cf75dc2018-03-17 00:38:03 -0400504 using face::FaceState;
505
Eric Newberry1b4ba052016-10-07 23:04:07 -0700506 FaceId faceId = face.getId();
507 m_faceStateChangeConn[faceId] = face.afterStateChange.connect(
Davide Pesavento3cf75dc2018-03-17 00:38:03 -0400508 [this, faceId, &face] (FaceState oldState, FaceState newState) {
509 if (newState == FaceState::UP) {
Eric Newberry1b4ba052016-10-07 23:04:07 -0700510 notifyFaceEvent(face, ndn::nfd::FACE_EVENT_UP);
511 }
Davide Pesavento3cf75dc2018-03-17 00:38:03 -0400512 else if (newState == FaceState::DOWN) {
Eric Newberry1b4ba052016-10-07 23:04:07 -0700513 notifyFaceEvent(face, ndn::nfd::FACE_EVENT_DOWN);
514 }
Davide Pesavento3cf75dc2018-03-17 00:38:03 -0400515 else if (newState == FaceState::CLOSED) {
516 // cannot use face.getId() because it may already be reset to INVALID_FACEID
Eric Newberry1b4ba052016-10-07 23:04:07 -0700517 m_faceStateChangeConn.erase(faceId);
518 }
519 });
Yanbiao Li73860e32015-08-19 16:30:16 -0700520}
521
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200522} // namespace nfd