blob: 34eb8e6f964de537190a708184929d3b380788c4 [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 Pesavento45c1f6a2025-01-01 19:30:30 -05003 * Copyright (c) 2014-2025, 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
Davide Pesavento2c9d2ca2024-01-27 16:36:51 -050039#include <limits>
40
Yanbiao Li73860e32015-08-19 16:30:16 -070041namespace nfd {
42
Davide Pesaventoa3148082018-04-12 18:21:54 -040043NFD_LOG_INIT(FaceManager);
Yanbiao Li73860e32015-08-19 16:30:16 -070044
Junxiao Shiea47bde2017-01-26 17:49:16 +000045FaceManager::FaceManager(FaceSystem& faceSystem,
Davide Pesavento78ddcab2019-02-28 22:00:03 -050046 Dispatcher& dispatcher, CommandAuthenticator& authenticator)
47 : ManagerBase("faces", dispatcher, authenticator)
Junxiao Shiea47bde2017-01-26 17:49:16 +000048 , m_faceSystem(faceSystem)
49 , m_faceTable(faceSystem.getFaceTable())
Yanbiao Li73860e32015-08-19 16:30:16 -070050{
Yanbiao Li58ba3f92017-02-15 14:27:18 +000051 // register handlers for ControlCommand
Davide Pesavento1db1bb62025-01-06 01:23:41 -050052 registerCommandHandler<ndn::nfd::FaceCreateCommand>([this] (auto&&, auto&&, auto&&... args) {
53 createFace(std::forward<decltype(args)>(args)...);
54 });
55 registerCommandHandler<ndn::nfd::FaceUpdateCommand>([this] (auto&&, auto&&... args) {
56 updateFace(std::forward<decltype(args)>(args)...);
57 });
58 registerCommandHandler<ndn::nfd::FaceDestroyCommand>([this] (auto&&, auto&&, auto&&... args) {
59 destroyFace(std::forward<decltype(args)>(args)...);
60 });
Yanbiao Li73860e32015-08-19 16:30:16 -070061
Yanbiao Li58ba3f92017-02-15 14:27:18 +000062 // register handlers for StatusDataset
Davide Pesaventoae430302023-05-11 01:42:46 -040063 registerStatusDatasetHandler("list",
64 [this] (auto&&, auto&&, auto&&... args) { listFaces(std::forward<decltype(args)>(args)...); });
65 registerStatusDatasetHandler("channels",
66 [this] (auto&&, auto&&, auto&&... args) { listChannels(std::forward<decltype(args)>(args)...); });
67 registerStatusDatasetHandler("query",
68 [this] (auto&&, auto&&... args) { queryFaces(std::forward<decltype(args)>(args)...); });
Yanbiao Li73860e32015-08-19 16:30:16 -070069
Yanbiao Li58ba3f92017-02-15 14:27:18 +000070 // register notification stream
Junxiao Shiae04d342016-07-19 13:20:22 +000071 m_postNotification = registerNotificationStream("events");
Eric Newberry1b4ba052016-10-07 23:04:07 -070072 m_faceAddConn = m_faceTable.afterAdd.connect([this] (const Face& face) {
73 connectFaceStateChangeSignal(face);
74 notifyFaceEvent(face, ndn::nfd::FACE_EVENT_CREATED);
75 });
76 m_faceRemoveConn = m_faceTable.beforeRemove.connect([this] (const Face& face) {
77 notifyFaceEvent(face, ndn::nfd::FACE_EVENT_DESTROYED);
78 });
Yanbiao Li73860e32015-08-19 16:30:16 -070079}
80
81void
Davide Pesavento28181322018-11-08 16:44:50 -050082FaceManager::createFace(const ControlParameters& parameters,
Davide Pesavento45c1f6a2025-01-01 19:30:30 -050083 const CommandContinuation& done)
Yanbiao Li73860e32015-08-19 16:30:16 -070084{
Eric Newberry78e32b02017-04-01 14:34:44 +000085 FaceUri remoteUri;
86 if (!remoteUri.parse(parameters.getUri())) {
87 NFD_LOG_TRACE("failed to parse remote URI: " << parameters.getUri());
Eric Newberry42602412016-08-27 09:33:18 -070088 done(ControlResponse(400, "Malformed command"));
89 return;
Yanbiao Li73860e32015-08-19 16:30:16 -070090 }
91
Eric Newberry78e32b02017-04-01 14:34:44 +000092 if (!remoteUri.isCanonical()) {
93 NFD_LOG_TRACE("received non-canonical remote URI: " << remoteUri.toString());
94 done(ControlResponse(400, "Non-canonical remote URI"));
Eric Newberry42602412016-08-27 09:33:18 -070095 return;
Yanbiao Li73860e32015-08-19 16:30:16 -070096 }
97
Davide Pesaventob7bfcb92022-05-22 23:55:23 -040098 std::optional<FaceUri> localUri;
Eric Newberry78e32b02017-04-01 14:34:44 +000099 if (parameters.hasLocalUri()) {
100 localUri = FaceUri{};
101
102 if (!localUri->parse(parameters.getLocalUri())) {
103 NFD_LOG_TRACE("failed to parse local URI: " << parameters.getLocalUri());
104 done(ControlResponse(400, "Malformed command"));
105 return;
106 }
107
108 if (!localUri->isCanonical()) {
109 NFD_LOG_TRACE("received non-canonical local URI: " << localUri->toString());
110 done(ControlResponse(400, "Non-canonical local URI"));
111 return;
112 }
113 }
114
Davide Pesaventoe5eebad2017-04-06 20:23:26 -0400115 face::ProtocolFactory* factory = m_faceSystem.getFactoryByScheme(remoteUri.getScheme());
Junxiao Shib8590312016-12-29 21:22:25 +0000116 if (factory == nullptr) {
Eric Newberry78e32b02017-04-01 14:34:44 +0000117 NFD_LOG_TRACE("received create request for unsupported protocol: " << remoteUri.getScheme());
Eric Newberry42602412016-08-27 09:33:18 -0700118 done(ControlResponse(406, "Unsupported protocol"));
119 return;
Yanbiao Li73860e32015-08-19 16:30:16 -0700120 }
121
Davide Pesavento15b55052018-01-27 19:09:28 -0500122 face::FaceParams faceParams;
123 faceParams.persistency = parameters.getFacePersistency();
Eric Newberry0c3e57b2018-01-25 20:54:46 -0700124 if (parameters.hasBaseCongestionMarkingInterval()) {
125 faceParams.baseCongestionMarkingInterval = parameters.getBaseCongestionMarkingInterval();
126 }
127 if (parameters.hasDefaultCongestionThreshold()) {
128 faceParams.defaultCongestionThreshold = parameters.getDefaultCongestionThreshold();
129 }
Eric Newberry812d6152018-06-06 15:06:01 -0700130 if (parameters.hasMtu()) {
Eric Newberry13ff2592020-03-06 17:32:29 -0800131 // The face system limits MTUs to ssize_t, but the management protocol uses uint64_t
Eric Newberrycb6551e2020-03-02 14:12:16 -0800132 faceParams.mtu = std::min<uint64_t>(std::numeric_limits<ssize_t>::max(), parameters.getMtu());
Eric Newberry812d6152018-06-06 15:06:01 -0700133 }
Davide Pesavento15b55052018-01-27 19:09:28 -0500134 faceParams.wantLocalFields = parameters.hasFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED) &&
135 parameters.getFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED);
136 faceParams.wantLpReliability = parameters.hasFlagBit(ndn::nfd::BIT_LP_RELIABILITY_ENABLED) &&
137 parameters.getFlagBit(ndn::nfd::BIT_LP_RELIABILITY_ENABLED);
Eric Newberry0c3e57b2018-01-25 20:54:46 -0700138 if (parameters.hasFlagBit(ndn::nfd::BIT_CONGESTION_MARKING_ENABLED)) {
139 faceParams.wantCongestionMarking = parameters.getFlagBit(ndn::nfd::BIT_CONGESTION_MARKING_ENABLED);
140 }
Yanbiao Li73860e32015-08-19 16:30:16 -0700141 try {
Davide Pesavento15b55052018-01-27 19:09:28 -0500142 factory->createFace({remoteUri, localUri, faceParams},
Davide Pesavento28181322018-11-08 16:44:50 -0500143 [this, parameters, done] (const auto& face) {
144 this->afterCreateFaceSuccess(face, parameters, done);
145 },
146 [done] (uint32_t status, const std::string& reason) {
147 NFD_LOG_DEBUG("Face creation failed: " << reason);
148 done(ControlResponse(status, reason));
149 });
Yanbiao Li73860e32015-08-19 16:30:16 -0700150 }
151 catch (const std::runtime_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 catch (const std::logic_error& error) {
Eric Newberry42602412016-08-27 09:33:18 -0700157 NFD_LOG_ERROR("Face creation failed: " << error.what());
158 done(ControlResponse(500, "Face creation failed due to internal error"));
159 return;
Yanbiao Li73860e32015-08-19 16:30:16 -0700160 }
161}
162
Davide Pesavento28181322018-11-08 16:44:50 -0500163template<typename T>
164static void
165copyMtu(const Face& face, T& to)
166{
Eric Newberrycb6551e2020-03-02 14:12:16 -0800167 if (face.getMtu() >= 0) {
168 to.setMtu(std::min<size_t>(face.getMtu(), ndn::MAX_NDN_PACKET_SIZE));
Davide Pesavento28181322018-11-08 16:44:50 -0500169 }
Eric Newberrycb6551e2020-03-02 14:12:16 -0800170 else if (face.getMtu() == face::MTU_UNLIMITED) {
Davide Pesavento28181322018-11-08 16:44:50 -0500171 to.setMtu(ndn::MAX_NDN_PACKET_SIZE);
172 }
173}
174
175static ControlParameters
176makeUpdateFaceResponse(const Face& face)
177{
178 ControlParameters params;
179 params.setFaceId(face.getId())
180 .setFacePersistency(face.getPersistency());
Eric Newberry13ff2592020-03-06 17:32:29 -0800181 copyMtu(face, params);
Davide Pesavento28181322018-11-08 16:44:50 -0500182
183 auto linkService = dynamic_cast<face::GenericLinkService*>(face.getLinkService());
184 if (linkService != nullptr) {
185 const auto& options = linkService->getOptions();
186 params.setBaseCongestionMarkingInterval(options.baseCongestionMarkingInterval)
187 .setDefaultCongestionThreshold(options.defaultCongestionThreshold)
188 .setFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED, options.allowLocalFields, false)
189 .setFlagBit(ndn::nfd::BIT_LP_RELIABILITY_ENABLED, options.reliabilityOptions.isEnabled, false)
190 .setFlagBit(ndn::nfd::BIT_CONGESTION_MARKING_ENABLED, options.allowCongestionMarking, false);
191 }
192
193 return params;
194}
195
196static ControlParameters
197makeCreateFaceResponse(const Face& face)
198{
199 ControlParameters params = makeUpdateFaceResponse(face);
200 params.setUri(face.getRemoteUri().toString())
201 .setLocalUri(face.getLocalUri().toString());
202
Davide Pesavento28181322018-11-08 16:44:50 -0500203 return params;
204}
205
Yanbiao Li73860e32015-08-19 16:30:16 -0700206void
Davide Pesavento28181322018-11-08 16:44:50 -0500207FaceManager::afterCreateFaceSuccess(const shared_ptr<Face>& face,
208 const ControlParameters& parameters,
Davide Pesavento45c1f6a2025-01-01 19:30:30 -0500209 const CommandContinuation& done)
Yanbiao Li73860e32015-08-19 16:30:16 -0700210{
Davide Pesavento28181322018-11-08 16:44:50 -0500211 if (face->getId() != face::INVALID_FACEID) { // Face already exists
Yanbiao Li58ba3f92017-02-15 14:27:18 +0000212 NFD_LOG_TRACE("Attempted to create duplicate face of " << face->getId());
Davide Pesavento28181322018-11-08 16:44:50 -0500213 ControlParameters response = makeCreateFaceResponse(*face);
Yanbiao Li58ba3f92017-02-15 14:27:18 +0000214 done(ControlResponse(409, "Face with remote URI already exists").setBody(response.wireEncode()));
215 return;
216 }
217
Eric Newberryf40551a2016-09-05 15:41:16 -0700218 // If scope non-local and flags set to enable local fields, request shouldn't
219 // have made it this far
220 BOOST_ASSERT(face->getScope() == ndn::nfd::FACE_SCOPE_LOCAL ||
221 !parameters.hasFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED) ||
222 (parameters.hasFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED) &&
223 !parameters.getFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED)));
224
225 m_faceTable.add(face);
226
Davide Pesavento28181322018-11-08 16:44:50 -0500227 ControlParameters response = makeCreateFaceResponse(*face);
Eric Newberry42602412016-08-27 09:33:18 -0700228 done(ControlResponse(200, "OK").setBody(response.wireEncode()));
229}
230
Davide Pesavento28181322018-11-08 16:44:50 -0500231static void
232updateLinkServiceOptions(Face& face, const ControlParameters& parameters)
Eric Newberry42602412016-08-27 09:33:18 -0700233{
Davide Pesavento28181322018-11-08 16:44:50 -0500234 auto linkService = dynamic_cast<face::GenericLinkService*>(face.getLinkService());
235 if (linkService == nullptr) {
236 return;
237 }
238 auto options = linkService->getOptions();
Eric Newberry42602412016-08-27 09:33:18 -0700239
Davide Pesavento28181322018-11-08 16:44:50 -0500240 if (parameters.hasFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED) &&
241 face.getScope() == ndn::nfd::FACE_SCOPE_LOCAL) {
242 options.allowLocalFields = parameters.getFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED);
243 }
244 if (parameters.hasFlagBit(ndn::nfd::BIT_LP_RELIABILITY_ENABLED)) {
245 options.reliabilityOptions.isEnabled = parameters.getFlagBit(ndn::nfd::BIT_LP_RELIABILITY_ENABLED);
246 }
247 if (parameters.hasFlagBit(ndn::nfd::BIT_CONGESTION_MARKING_ENABLED)) {
248 options.allowCongestionMarking = parameters.getFlagBit(ndn::nfd::BIT_CONGESTION_MARKING_ENABLED);
249 }
250 if (parameters.hasBaseCongestionMarkingInterval()) {
251 options.baseCongestionMarkingInterval = parameters.getBaseCongestionMarkingInterval();
252 }
253 if (parameters.hasDefaultCongestionThreshold()) {
254 options.defaultCongestionThreshold = parameters.getDefaultCongestionThreshold();
255 }
256
Eric Newberry13ff2592020-03-06 17:32:29 -0800257 if (parameters.hasMtu()) {
258 // The face system limits MTUs to ssize_t, but the management protocol uses uint64_t
259 options.overrideMtu = std::min<uint64_t>(std::numeric_limits<ssize_t>::max(), parameters.getMtu());
260 }
261
Davide Pesavento28181322018-11-08 16:44:50 -0500262 linkService->setOptions(options);
Yanbiao Li73860e32015-08-19 16:30:16 -0700263}
264
265void
Davide Pesavento28181322018-11-08 16:44:50 -0500266FaceManager::updateFace(const Interest& interest,
Eric Newberryb5aa7f52016-09-03 20:36:12 -0700267 const ControlParameters& parameters,
Davide Pesavento45c1f6a2025-01-01 19:30:30 -0500268 const CommandContinuation& done)
Eric Newberryb5aa7f52016-09-03 20:36:12 -0700269{
270 FaceId faceId = parameters.getFaceId();
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400271 if (faceId == face::INVALID_FACEID) { // Self-update
Davide Pesavento28181322018-11-08 16:44:50 -0500272 auto incomingFaceIdTag = interest.getTag<lp::IncomingFaceIdTag>();
Eric Newberryb5aa7f52016-09-03 20:36:12 -0700273 if (incomingFaceIdTag == nullptr) {
274 NFD_LOG_TRACE("unable to determine face for self-update");
275 done(ControlResponse(404, "No FaceId specified and IncomingFaceId not available"));
276 return;
277 }
278 faceId = *incomingFaceIdTag;
279 }
280
281 Face* face = m_faceTable.get(faceId);
Eric Newberryb5aa7f52016-09-03 20:36:12 -0700282 if (face == nullptr) {
283 NFD_LOG_TRACE("invalid face specified");
284 done(ControlResponse(404, "Specified face does not exist"));
285 return;
286 }
287
288 // Verify validity of requested changes
289 ControlParameters response;
290 bool areParamsValid = true;
291
Eric Newberryb5aa7f52016-09-03 20:36:12 -0700292 if (parameters.hasFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED) &&
293 parameters.getFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED) &&
294 face->getScope() != ndn::nfd::FACE_SCOPE_LOCAL) {
295 NFD_LOG_TRACE("received request to enable local fields on non-local face");
296 areParamsValid = false;
297 response.setFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED,
298 parameters.getFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED));
299 }
300
Yanbiao Li58ba3f92017-02-15 14:27:18 +0000301 // check whether the requested FacePersistency change is valid if it's present
302 if (parameters.hasFacePersistency()) {
303 auto persistency = parameters.getFacePersistency();
304 if (!face->getTransport()->canChangePersistencyTo(persistency)) {
305 NFD_LOG_TRACE("cannot change face persistency to " << persistency);
306 areParamsValid = false;
307 response.setFacePersistency(persistency);
308 }
309 }
310
Eric Newberry13ff2592020-03-06 17:32:29 -0800311 // check whether the requested MTU override is valid (if it's present)
312 if (parameters.hasMtu()) {
313 auto mtu = parameters.getMtu();
314 // The face system limits MTUs to ssize_t, but the management protocol uses uint64_t
315 auto actualMtu = std::min<uint64_t>(std::numeric_limits<ssize_t>::max(), mtu);
316 auto linkService = dynamic_cast<face::GenericLinkService*>(face->getLinkService());
317 if (linkService == nullptr || !linkService->canOverrideMtuTo(actualMtu)) {
318 NFD_LOG_TRACE("cannot override face MTU to " << mtu);
319 areParamsValid = false;
320 response.setMtu(mtu);
321 }
322 }
323
Eric Newberryb5aa7f52016-09-03 20:36:12 -0700324 if (!areParamsValid) {
325 done(ControlResponse(409, "Invalid properties specified").setBody(response.wireEncode()));
326 return;
327 }
328
329 // All specified properties are valid, so make changes
Yanbiao Li58ba3f92017-02-15 14:27:18 +0000330 if (parameters.hasFacePersistency()) {
331 face->setPersistency(parameters.getFacePersistency());
332 }
Davide Pesavento28181322018-11-08 16:44:50 -0500333 updateLinkServiceOptions(*face, parameters);
Eric Newberryb5aa7f52016-09-03 20:36:12 -0700334
Davide Pesavento28181322018-11-08 16:44:50 -0500335 // Prepare and send ControlResponse
336 response = makeUpdateFaceResponse(*face);
Eric Newberryb5aa7f52016-09-03 20:36:12 -0700337 done(ControlResponse(200, "OK").setBody(response.wireEncode()));
338}
339
340void
Davide Pesavento28181322018-11-08 16:44:50 -0500341FaceManager::destroyFace(const ControlParameters& parameters,
Davide Pesavento45c1f6a2025-01-01 19:30:30 -0500342 const CommandContinuation& done)
Junxiao Shi40cb61c2015-09-23 18:36:45 -0700343{
Junxiao Shi5b43f9a2016-07-19 13:15:56 +0000344 Face* face = m_faceTable.get(parameters.getFaceId());
345 if (face != nullptr) {
346 face->close();
Junxiao Shi40cb61c2015-09-23 18:36:45 -0700347 }
348
349 done(ControlResponse(200, "OK").setBody(parameters.wireEncode()));
350}
351
Davide Pesavento28181322018-11-08 16:44:50 -0500352template<typename T>
353static void
354copyFaceProperties(const Face& face, T& to)
Eric Newberryb5aa7f52016-09-03 20:36:12 -0700355{
Davide Pesavento28181322018-11-08 16:44:50 -0500356 to.setFaceId(face.getId())
357 .setRemoteUri(face.getRemoteUri().toString())
358 .setLocalUri(face.getLocalUri().toString())
359 .setFaceScope(face.getScope())
360 .setFacePersistency(face.getPersistency())
361 .setLinkType(face.getLinkType());
Eric Newberryb5aa7f52016-09-03 20:36:12 -0700362
Davide Pesavento28181322018-11-08 16:44:50 -0500363 auto linkService = dynamic_cast<face::GenericLinkService*>(face.getLinkService());
364 if (linkService != nullptr) {
365 const auto& options = linkService->getOptions();
366 to.setFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED, options.allowLocalFields)
367 .setFlagBit(ndn::nfd::BIT_LP_RELIABILITY_ENABLED, options.reliabilityOptions.isEnabled)
368 .setFlagBit(ndn::nfd::BIT_CONGESTION_MARKING_ENABLED, options.allowCongestionMarking);
Eric Newberryb5aa7f52016-09-03 20:36:12 -0700369 }
Eric Newberryb5aa7f52016-09-03 20:36:12 -0700370}
371
Davide Pesavento28181322018-11-08 16:44:50 -0500372static ndn::nfd::FaceStatus
Davide Pesavento412c9822021-07-02 00:21:05 -0400373makeFaceStatus(const Face& face, const time::steady_clock::time_point& now)
Yanbiao Li58ba3f92017-02-15 14:27:18 +0000374{
Davide Pesavento28181322018-11-08 16:44:50 -0500375 ndn::nfd::FaceStatus status;
376 copyFaceProperties(face, status);
377
378 auto expirationTime = face.getExpirationTime();
Davide Pesavento412c9822021-07-02 00:21:05 -0400379 if (expirationTime != time::steady_clock::time_point::max()) {
Davide Pesavento28181322018-11-08 16:44:50 -0500380 status.setExpirationPeriod(std::max(0_ms,
381 time::duration_cast<time::milliseconds>(expirationTime - now)));
382 }
383
Yanbiao Li58ba3f92017-02-15 14:27:18 +0000384 auto linkService = dynamic_cast<face::GenericLinkService*>(face.getLinkService());
Davide Pesavento28181322018-11-08 16:44:50 -0500385 if (linkService != nullptr) {
386 const auto& options = linkService->getOptions();
387 status.setBaseCongestionMarkingInterval(options.baseCongestionMarkingInterval)
388 .setDefaultCongestionThreshold(options.defaultCongestionThreshold);
Eric Newberry812d6152018-06-06 15:06:01 -0700389 }
390
Davide Pesavento28181322018-11-08 16:44:50 -0500391 copyMtu(face, status);
392
393 const auto& counters = face.getCounters();
394 status.setNInInterests(counters.nInInterests)
395 .setNOutInterests(counters.nOutInterests)
396 .setNInData(counters.nInData)
397 .setNOutData(counters.nOutData)
398 .setNInNacks(counters.nInNacks)
399 .setNOutNacks(counters.nOutNacks)
400 .setNInBytes(counters.nInBytes)
401 .setNOutBytes(counters.nOutBytes);
402
403 return status;
Yanbiao Li58ba3f92017-02-15 14:27:18 +0000404}
405
Eric Newberryb5aa7f52016-09-03 20:36:12 -0700406void
Davide Pesavento28181322018-11-08 16:44:50 -0500407FaceManager::listFaces(ndn::mgmt::StatusDatasetContext& context)
Yanbiao Li73860e32015-08-19 16:30:16 -0700408{
Eric Newberryc64d30a2015-12-26 11:07:27 -0700409 auto now = time::steady_clock::now();
Davide Pesavento28181322018-11-08 16:44:50 -0500410 for (const auto& face : m_faceTable) {
411 ndn::nfd::FaceStatus status = makeFaceStatus(face, now);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700412 context.append(status.wireEncode());
Yanbiao Li73860e32015-08-19 16:30:16 -0700413 }
414 context.end();
415}
416
417void
Davide Pesavento28181322018-11-08 16:44:50 -0500418FaceManager::listChannels(ndn::mgmt::StatusDatasetContext& context)
Yanbiao Li73860e32015-08-19 16:30:16 -0700419{
Davide Pesavento28181322018-11-08 16:44:50 -0500420 auto factories = m_faceSystem.listProtocolFactories();
Davide Pesaventoe5eebad2017-04-06 20:23:26 -0400421 for (const auto* factory : factories) {
Junxiao Shib8590312016-12-29 21:22:25 +0000422 for (const auto& channel : factory->getChannels()) {
423 ndn::nfd::ChannelStatus entry;
424 entry.setLocalUri(channel->getUri().toString());
425 context.append(entry.wireEncode());
Yanbiao Li73860e32015-08-19 16:30:16 -0700426 }
427 }
Yanbiao Li73860e32015-08-19 16:30:16 -0700428 context.end();
429}
430
Davide Pesavento28181322018-11-08 16:44:50 -0500431static bool
432matchFilter(const ndn::nfd::FaceQueryFilter& filter, const Face& face)
Yanbiao Li73860e32015-08-19 16:30:16 -0700433{
434 if (filter.hasFaceId() &&
Junxiao Shib84e6742016-07-19 13:16:22 +0000435 filter.getFaceId() != static_cast<uint64_t>(face.getId())) {
Yanbiao Li73860e32015-08-19 16:30:16 -0700436 return false;
437 }
438
439 if (filter.hasUriScheme() &&
Junxiao Shib84e6742016-07-19 13:16:22 +0000440 filter.getUriScheme() != face.getRemoteUri().getScheme() &&
441 filter.getUriScheme() != face.getLocalUri().getScheme()) {
Yanbiao Li73860e32015-08-19 16:30:16 -0700442 return false;
443 }
444
445 if (filter.hasRemoteUri() &&
Junxiao Shib84e6742016-07-19 13:16:22 +0000446 filter.getRemoteUri() != face.getRemoteUri().toString()) {
Yanbiao Li73860e32015-08-19 16:30:16 -0700447 return false;
448 }
449
450 if (filter.hasLocalUri() &&
Junxiao Shib84e6742016-07-19 13:16:22 +0000451 filter.getLocalUri() != face.getLocalUri().toString()) {
Yanbiao Li73860e32015-08-19 16:30:16 -0700452 return false;
453 }
454
455 if (filter.hasFaceScope() &&
Junxiao Shib84e6742016-07-19 13:16:22 +0000456 filter.getFaceScope() != face.getScope()) {
Yanbiao Li73860e32015-08-19 16:30:16 -0700457 return false;
458 }
459
460 if (filter.hasFacePersistency() &&
Junxiao Shib84e6742016-07-19 13:16:22 +0000461 filter.getFacePersistency() != face.getPersistency()) {
Yanbiao Li73860e32015-08-19 16:30:16 -0700462 return false;
463 }
464
465 if (filter.hasLinkType() &&
Junxiao Shib84e6742016-07-19 13:16:22 +0000466 filter.getLinkType() != face.getLinkType()) {
Yanbiao Li73860e32015-08-19 16:30:16 -0700467 return false;
468 }
469
470 return true;
471}
472
Junxiao Shicde37ad2015-12-24 01:02:05 -0700473void
Davide Pesavento28181322018-11-08 16:44:50 -0500474FaceManager::queryFaces(const Interest& interest,
475 ndn::mgmt::StatusDatasetContext& context)
Junxiao Shida93f1f2015-11-11 06:13:16 -0700476{
Davide Pesavento28181322018-11-08 16:44:50 -0500477 ndn::nfd::FaceQueryFilter faceFilter;
478 try {
479 faceFilter.wireDecode(interest.getName()[-1].blockFromValue());
Eric Newberry1b4ba052016-10-07 23:04:07 -0700480 }
Davide Pesavento28181322018-11-08 16:44:50 -0500481 catch (const tlv::Error& e) {
482 NFD_LOG_DEBUG("Malformed query filter: " << e.what());
483 return context.reject(ControlResponse(400, "Malformed filter"));
484 }
485
486 auto now = time::steady_clock::now();
487 for (const auto& face : m_faceTable) {
488 if (matchFilter(faceFilter, face)) {
489 ndn::nfd::FaceStatus status = makeFaceStatus(face, now);
490 context.append(status.wireEncode());
491 }
492 }
493 context.end();
Junxiao Shida93f1f2015-11-11 06:13:16 -0700494}
495
496void
Eric Newberry1b4ba052016-10-07 23:04:07 -0700497FaceManager::notifyFaceEvent(const Face& face, ndn::nfd::FaceEventKind kind)
Yanbiao Li73860e32015-08-19 16:30:16 -0700498{
499 ndn::nfd::FaceEventNotification notification;
Eric Newberry1b4ba052016-10-07 23:04:07 -0700500 notification.setKind(kind);
Davide Pesavento28181322018-11-08 16:44:50 -0500501 copyFaceProperties(face, notification);
Yanbiao Li73860e32015-08-19 16:30:16 -0700502
Junxiao Shiae04d342016-07-19 13:20:22 +0000503 m_postNotification(notification.wireEncode());
Yanbiao Li73860e32015-08-19 16:30:16 -0700504}
505
506void
Eric Newberry1b4ba052016-10-07 23:04:07 -0700507FaceManager::connectFaceStateChangeSignal(const Face& face)
Yanbiao Li73860e32015-08-19 16:30:16 -0700508{
Davide Pesavento3cf75dc2018-03-17 00:38:03 -0400509 using face::FaceState;
510
Eric Newberry1b4ba052016-10-07 23:04:07 -0700511 FaceId faceId = face.getId();
512 m_faceStateChangeConn[faceId] = face.afterStateChange.connect(
Davide Pesavento3cf75dc2018-03-17 00:38:03 -0400513 [this, faceId, &face] (FaceState oldState, FaceState newState) {
514 if (newState == FaceState::UP) {
Eric Newberry1b4ba052016-10-07 23:04:07 -0700515 notifyFaceEvent(face, ndn::nfd::FACE_EVENT_UP);
516 }
Davide Pesavento3cf75dc2018-03-17 00:38:03 -0400517 else if (newState == FaceState::DOWN) {
Eric Newberry1b4ba052016-10-07 23:04:07 -0700518 notifyFaceEvent(face, ndn::nfd::FACE_EVENT_DOWN);
519 }
Davide Pesavento3cf75dc2018-03-17 00:38:03 -0400520 else if (newState == FaceState::CLOSED) {
521 // cannot use face.getId() because it may already be reset to INVALID_FACEID
Eric Newberry1b4ba052016-10-07 23:04:07 -0700522 m_faceStateChangeConn.erase(faceId);
523 }
524 });
Yanbiao Li73860e32015-08-19 16:30:16 -0700525}
526
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200527} // namespace nfd