blob: 839a6fb43f8d43a660c766102d2812af76d7edbc [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 Pesaventob7bfcb92022-05-22 23:55:23 -04003 * Copyright (c) 2014-2022, 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",
51 std::bind(&FaceManager::createFace, this, _4, _5));
52 registerCommandHandler<ndn::nfd::FaceUpdateCommand>("update",
53 std::bind(&FaceManager::updateFace, this, _3, _4, _5));
54 registerCommandHandler<ndn::nfd::FaceDestroyCommand>("destroy",
55 std::bind(&FaceManager::destroyFace, this, _4, _5));
Yanbiao Li73860e32015-08-19 16:30:16 -070056
Yanbiao Li58ba3f92017-02-15 14:27:18 +000057 // register handlers for StatusDataset
Davide Pesavento412c9822021-07-02 00:21:05 -040058 registerStatusDatasetHandler("list", std::bind(&FaceManager::listFaces, this, _3));
59 registerStatusDatasetHandler("channels", std::bind(&FaceManager::listChannels, this, _3));
60 registerStatusDatasetHandler("query", std::bind(&FaceManager::queryFaces, this, _2, _3));
Yanbiao Li73860e32015-08-19 16:30:16 -070061
Yanbiao Li58ba3f92017-02-15 14:27:18 +000062 // register notification stream
Junxiao Shiae04d342016-07-19 13:20:22 +000063 m_postNotification = registerNotificationStream("events");
Eric Newberry1b4ba052016-10-07 23:04:07 -070064 m_faceAddConn = m_faceTable.afterAdd.connect([this] (const Face& face) {
65 connectFaceStateChangeSignal(face);
66 notifyFaceEvent(face, ndn::nfd::FACE_EVENT_CREATED);
67 });
68 m_faceRemoveConn = m_faceTable.beforeRemove.connect([this] (const Face& face) {
69 notifyFaceEvent(face, ndn::nfd::FACE_EVENT_DESTROYED);
70 });
Yanbiao Li73860e32015-08-19 16:30:16 -070071}
72
73void
Davide Pesavento28181322018-11-08 16:44:50 -050074FaceManager::createFace(const ControlParameters& parameters,
Yanbiao Li73860e32015-08-19 16:30:16 -070075 const ndn::mgmt::CommandContinuation& done)
76{
Eric Newberry78e32b02017-04-01 14:34:44 +000077 FaceUri remoteUri;
78 if (!remoteUri.parse(parameters.getUri())) {
79 NFD_LOG_TRACE("failed to parse remote URI: " << parameters.getUri());
Eric Newberry42602412016-08-27 09:33:18 -070080 done(ControlResponse(400, "Malformed command"));
81 return;
Yanbiao Li73860e32015-08-19 16:30:16 -070082 }
83
Eric Newberry78e32b02017-04-01 14:34:44 +000084 if (!remoteUri.isCanonical()) {
85 NFD_LOG_TRACE("received non-canonical remote URI: " << remoteUri.toString());
86 done(ControlResponse(400, "Non-canonical remote URI"));
Eric Newberry42602412016-08-27 09:33:18 -070087 return;
Yanbiao Li73860e32015-08-19 16:30:16 -070088 }
89
Davide Pesaventob7bfcb92022-05-22 23:55:23 -040090 std::optional<FaceUri> localUri;
Eric Newberry78e32b02017-04-01 14:34:44 +000091 if (parameters.hasLocalUri()) {
92 localUri = FaceUri{};
93
94 if (!localUri->parse(parameters.getLocalUri())) {
95 NFD_LOG_TRACE("failed to parse local URI: " << parameters.getLocalUri());
96 done(ControlResponse(400, "Malformed command"));
97 return;
98 }
99
100 if (!localUri->isCanonical()) {
101 NFD_LOG_TRACE("received non-canonical local URI: " << localUri->toString());
102 done(ControlResponse(400, "Non-canonical local URI"));
103 return;
104 }
105 }
106
Davide Pesaventoe5eebad2017-04-06 20:23:26 -0400107 face::ProtocolFactory* factory = m_faceSystem.getFactoryByScheme(remoteUri.getScheme());
Junxiao Shib8590312016-12-29 21:22:25 +0000108 if (factory == nullptr) {
Eric Newberry78e32b02017-04-01 14:34:44 +0000109 NFD_LOG_TRACE("received create request for unsupported protocol: " << remoteUri.getScheme());
Eric Newberry42602412016-08-27 09:33:18 -0700110 done(ControlResponse(406, "Unsupported protocol"));
111 return;
Yanbiao Li73860e32015-08-19 16:30:16 -0700112 }
113
Davide Pesavento15b55052018-01-27 19:09:28 -0500114 face::FaceParams faceParams;
115 faceParams.persistency = parameters.getFacePersistency();
Eric Newberry0c3e57b2018-01-25 20:54:46 -0700116 if (parameters.hasBaseCongestionMarkingInterval()) {
117 faceParams.baseCongestionMarkingInterval = parameters.getBaseCongestionMarkingInterval();
118 }
119 if (parameters.hasDefaultCongestionThreshold()) {
120 faceParams.defaultCongestionThreshold = parameters.getDefaultCongestionThreshold();
121 }
Eric Newberry812d6152018-06-06 15:06:01 -0700122 if (parameters.hasMtu()) {
Eric Newberry13ff2592020-03-06 17:32:29 -0800123 // The face system limits MTUs to ssize_t, but the management protocol uses uint64_t
Eric Newberrycb6551e2020-03-02 14:12:16 -0800124 faceParams.mtu = std::min<uint64_t>(std::numeric_limits<ssize_t>::max(), parameters.getMtu());
Eric Newberry812d6152018-06-06 15:06:01 -0700125 }
Davide Pesavento15b55052018-01-27 19:09:28 -0500126 faceParams.wantLocalFields = parameters.hasFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED) &&
127 parameters.getFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED);
128 faceParams.wantLpReliability = parameters.hasFlagBit(ndn::nfd::BIT_LP_RELIABILITY_ENABLED) &&
129 parameters.getFlagBit(ndn::nfd::BIT_LP_RELIABILITY_ENABLED);
Eric Newberry0c3e57b2018-01-25 20:54:46 -0700130 if (parameters.hasFlagBit(ndn::nfd::BIT_CONGESTION_MARKING_ENABLED)) {
131 faceParams.wantCongestionMarking = parameters.getFlagBit(ndn::nfd::BIT_CONGESTION_MARKING_ENABLED);
132 }
Yanbiao Li73860e32015-08-19 16:30:16 -0700133 try {
Davide Pesavento15b55052018-01-27 19:09:28 -0500134 factory->createFace({remoteUri, localUri, faceParams},
Davide Pesavento28181322018-11-08 16:44:50 -0500135 [this, parameters, done] (const auto& face) {
136 this->afterCreateFaceSuccess(face, parameters, done);
137 },
138 [done] (uint32_t status, const std::string& reason) {
139 NFD_LOG_DEBUG("Face creation failed: " << reason);
140 done(ControlResponse(status, reason));
141 });
Yanbiao Li73860e32015-08-19 16:30:16 -0700142 }
143 catch (const std::runtime_error& error) {
Eric Newberry42602412016-08-27 09:33:18 -0700144 NFD_LOG_ERROR("Face creation failed: " << error.what());
145 done(ControlResponse(500, "Face creation failed due to internal error"));
146 return;
Yanbiao Li73860e32015-08-19 16:30:16 -0700147 }
148 catch (const std::logic_error& error) {
Eric Newberry42602412016-08-27 09:33:18 -0700149 NFD_LOG_ERROR("Face creation failed: " << error.what());
150 done(ControlResponse(500, "Face creation failed due to internal error"));
151 return;
Yanbiao Li73860e32015-08-19 16:30:16 -0700152 }
153}
154
Davide Pesavento28181322018-11-08 16:44:50 -0500155template<typename T>
156static void
157copyMtu(const Face& face, T& to)
158{
Eric Newberrycb6551e2020-03-02 14:12:16 -0800159 if (face.getMtu() >= 0) {
160 to.setMtu(std::min<size_t>(face.getMtu(), ndn::MAX_NDN_PACKET_SIZE));
Davide Pesavento28181322018-11-08 16:44:50 -0500161 }
Eric Newberrycb6551e2020-03-02 14:12:16 -0800162 else if (face.getMtu() == face::MTU_UNLIMITED) {
Davide Pesavento28181322018-11-08 16:44:50 -0500163 to.setMtu(ndn::MAX_NDN_PACKET_SIZE);
164 }
165}
166
167static ControlParameters
168makeUpdateFaceResponse(const Face& face)
169{
170 ControlParameters params;
171 params.setFaceId(face.getId())
172 .setFacePersistency(face.getPersistency());
Eric Newberry13ff2592020-03-06 17:32:29 -0800173 copyMtu(face, params);
Davide Pesavento28181322018-11-08 16:44:50 -0500174
175 auto linkService = dynamic_cast<face::GenericLinkService*>(face.getLinkService());
176 if (linkService != nullptr) {
177 const auto& options = linkService->getOptions();
178 params.setBaseCongestionMarkingInterval(options.baseCongestionMarkingInterval)
179 .setDefaultCongestionThreshold(options.defaultCongestionThreshold)
180 .setFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED, options.allowLocalFields, false)
181 .setFlagBit(ndn::nfd::BIT_LP_RELIABILITY_ENABLED, options.reliabilityOptions.isEnabled, false)
182 .setFlagBit(ndn::nfd::BIT_CONGESTION_MARKING_ENABLED, options.allowCongestionMarking, false);
183 }
184
185 return params;
186}
187
188static ControlParameters
189makeCreateFaceResponse(const Face& face)
190{
191 ControlParameters params = makeUpdateFaceResponse(face);
192 params.setUri(face.getRemoteUri().toString())
193 .setLocalUri(face.getLocalUri().toString());
194
Davide Pesavento28181322018-11-08 16:44:50 -0500195 return params;
196}
197
Yanbiao Li73860e32015-08-19 16:30:16 -0700198void
Davide Pesavento28181322018-11-08 16:44:50 -0500199FaceManager::afterCreateFaceSuccess(const shared_ptr<Face>& face,
200 const ControlParameters& parameters,
Yanbiao Li73860e32015-08-19 16:30:16 -0700201 const ndn::mgmt::CommandContinuation& done)
202{
Davide Pesavento28181322018-11-08 16:44:50 -0500203 if (face->getId() != face::INVALID_FACEID) { // Face already exists
Yanbiao Li58ba3f92017-02-15 14:27:18 +0000204 NFD_LOG_TRACE("Attempted to create duplicate face of " << face->getId());
Davide Pesavento28181322018-11-08 16:44:50 -0500205 ControlParameters response = makeCreateFaceResponse(*face);
Yanbiao Li58ba3f92017-02-15 14:27:18 +0000206 done(ControlResponse(409, "Face with remote URI already exists").setBody(response.wireEncode()));
207 return;
208 }
209
Eric Newberryf40551a2016-09-05 15:41:16 -0700210 // If scope non-local and flags set to enable local fields, request shouldn't
211 // have made it this far
212 BOOST_ASSERT(face->getScope() == ndn::nfd::FACE_SCOPE_LOCAL ||
213 !parameters.hasFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED) ||
214 (parameters.hasFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED) &&
215 !parameters.getFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED)));
216
217 m_faceTable.add(face);
218
Davide Pesavento28181322018-11-08 16:44:50 -0500219 ControlParameters response = makeCreateFaceResponse(*face);
Eric Newberry42602412016-08-27 09:33:18 -0700220 done(ControlResponse(200, "OK").setBody(response.wireEncode()));
221}
222
Davide Pesavento28181322018-11-08 16:44:50 -0500223static void
224updateLinkServiceOptions(Face& face, const ControlParameters& parameters)
Eric Newberry42602412016-08-27 09:33:18 -0700225{
Davide Pesavento28181322018-11-08 16:44:50 -0500226 auto linkService = dynamic_cast<face::GenericLinkService*>(face.getLinkService());
227 if (linkService == nullptr) {
228 return;
229 }
230 auto options = linkService->getOptions();
Eric Newberry42602412016-08-27 09:33:18 -0700231
Davide Pesavento28181322018-11-08 16:44:50 -0500232 if (parameters.hasFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED) &&
233 face.getScope() == ndn::nfd::FACE_SCOPE_LOCAL) {
234 options.allowLocalFields = parameters.getFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED);
235 }
236 if (parameters.hasFlagBit(ndn::nfd::BIT_LP_RELIABILITY_ENABLED)) {
237 options.reliabilityOptions.isEnabled = parameters.getFlagBit(ndn::nfd::BIT_LP_RELIABILITY_ENABLED);
238 }
239 if (parameters.hasFlagBit(ndn::nfd::BIT_CONGESTION_MARKING_ENABLED)) {
240 options.allowCongestionMarking = parameters.getFlagBit(ndn::nfd::BIT_CONGESTION_MARKING_ENABLED);
241 }
242 if (parameters.hasBaseCongestionMarkingInterval()) {
243 options.baseCongestionMarkingInterval = parameters.getBaseCongestionMarkingInterval();
244 }
245 if (parameters.hasDefaultCongestionThreshold()) {
246 options.defaultCongestionThreshold = parameters.getDefaultCongestionThreshold();
247 }
248
Eric Newberry13ff2592020-03-06 17:32:29 -0800249 if (parameters.hasMtu()) {
250 // The face system limits MTUs to ssize_t, but the management protocol uses uint64_t
251 options.overrideMtu = std::min<uint64_t>(std::numeric_limits<ssize_t>::max(), parameters.getMtu());
252 }
253
Davide Pesavento28181322018-11-08 16:44:50 -0500254 linkService->setOptions(options);
Yanbiao Li73860e32015-08-19 16:30:16 -0700255}
256
257void
Davide Pesavento28181322018-11-08 16:44:50 -0500258FaceManager::updateFace(const Interest& interest,
Eric Newberryb5aa7f52016-09-03 20:36:12 -0700259 const ControlParameters& parameters,
260 const ndn::mgmt::CommandContinuation& done)
261{
262 FaceId faceId = parameters.getFaceId();
Davide Pesavento28181322018-11-08 16:44:50 -0500263 if (faceId == 0) { // Self-update
264 auto incomingFaceIdTag = interest.getTag<lp::IncomingFaceIdTag>();
Eric Newberryb5aa7f52016-09-03 20:36:12 -0700265 if (incomingFaceIdTag == nullptr) {
266 NFD_LOG_TRACE("unable to determine face for self-update");
267 done(ControlResponse(404, "No FaceId specified and IncomingFaceId not available"));
268 return;
269 }
270 faceId = *incomingFaceIdTag;
271 }
272
273 Face* face = m_faceTable.get(faceId);
Eric Newberryb5aa7f52016-09-03 20:36:12 -0700274 if (face == nullptr) {
275 NFD_LOG_TRACE("invalid face specified");
276 done(ControlResponse(404, "Specified face does not exist"));
277 return;
278 }
279
280 // Verify validity of requested changes
281 ControlParameters response;
282 bool areParamsValid = true;
283
Eric Newberryb5aa7f52016-09-03 20:36:12 -0700284 if (parameters.hasFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED) &&
285 parameters.getFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED) &&
286 face->getScope() != ndn::nfd::FACE_SCOPE_LOCAL) {
287 NFD_LOG_TRACE("received request to enable local fields on non-local face");
288 areParamsValid = false;
289 response.setFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED,
290 parameters.getFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED));
291 }
292
Yanbiao Li58ba3f92017-02-15 14:27:18 +0000293 // check whether the requested FacePersistency change is valid if it's present
294 if (parameters.hasFacePersistency()) {
295 auto persistency = parameters.getFacePersistency();
296 if (!face->getTransport()->canChangePersistencyTo(persistency)) {
297 NFD_LOG_TRACE("cannot change face persistency to " << persistency);
298 areParamsValid = false;
299 response.setFacePersistency(persistency);
300 }
301 }
302
Eric Newberry13ff2592020-03-06 17:32:29 -0800303 // check whether the requested MTU override is valid (if it's present)
304 if (parameters.hasMtu()) {
305 auto mtu = parameters.getMtu();
306 // The face system limits MTUs to ssize_t, but the management protocol uses uint64_t
307 auto actualMtu = std::min<uint64_t>(std::numeric_limits<ssize_t>::max(), mtu);
308 auto linkService = dynamic_cast<face::GenericLinkService*>(face->getLinkService());
309 if (linkService == nullptr || !linkService->canOverrideMtuTo(actualMtu)) {
310 NFD_LOG_TRACE("cannot override face MTU to " << mtu);
311 areParamsValid = false;
312 response.setMtu(mtu);
313 }
314 }
315
Eric Newberryb5aa7f52016-09-03 20:36:12 -0700316 if (!areParamsValid) {
317 done(ControlResponse(409, "Invalid properties specified").setBody(response.wireEncode()));
318 return;
319 }
320
321 // All specified properties are valid, so make changes
Yanbiao Li58ba3f92017-02-15 14:27:18 +0000322 if (parameters.hasFacePersistency()) {
323 face->setPersistency(parameters.getFacePersistency());
324 }
Davide Pesavento28181322018-11-08 16:44:50 -0500325 updateLinkServiceOptions(*face, parameters);
Eric Newberryb5aa7f52016-09-03 20:36:12 -0700326
Davide Pesavento28181322018-11-08 16:44:50 -0500327 // Prepare and send ControlResponse
328 response = makeUpdateFaceResponse(*face);
Eric Newberryb5aa7f52016-09-03 20:36:12 -0700329 done(ControlResponse(200, "OK").setBody(response.wireEncode()));
330}
331
332void
Davide Pesavento28181322018-11-08 16:44:50 -0500333FaceManager::destroyFace(const ControlParameters& parameters,
Junxiao Shi40cb61c2015-09-23 18:36:45 -0700334 const ndn::mgmt::CommandContinuation& done)
335{
Junxiao Shi5b43f9a2016-07-19 13:15:56 +0000336 Face* face = m_faceTable.get(parameters.getFaceId());
337 if (face != nullptr) {
338 face->close();
Junxiao Shi40cb61c2015-09-23 18:36:45 -0700339 }
340
341 done(ControlResponse(200, "OK").setBody(parameters.wireEncode()));
342}
343
Davide Pesavento28181322018-11-08 16:44:50 -0500344template<typename T>
345static void
346copyFaceProperties(const Face& face, T& to)
Eric Newberryb5aa7f52016-09-03 20:36:12 -0700347{
Davide Pesavento28181322018-11-08 16:44:50 -0500348 to.setFaceId(face.getId())
349 .setRemoteUri(face.getRemoteUri().toString())
350 .setLocalUri(face.getLocalUri().toString())
351 .setFaceScope(face.getScope())
352 .setFacePersistency(face.getPersistency())
353 .setLinkType(face.getLinkType());
Eric Newberryb5aa7f52016-09-03 20:36:12 -0700354
Davide Pesavento28181322018-11-08 16:44:50 -0500355 auto linkService = dynamic_cast<face::GenericLinkService*>(face.getLinkService());
356 if (linkService != nullptr) {
357 const auto& options = linkService->getOptions();
358 to.setFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED, options.allowLocalFields)
359 .setFlagBit(ndn::nfd::BIT_LP_RELIABILITY_ENABLED, options.reliabilityOptions.isEnabled)
360 .setFlagBit(ndn::nfd::BIT_CONGESTION_MARKING_ENABLED, options.allowCongestionMarking);
Eric Newberryb5aa7f52016-09-03 20:36:12 -0700361 }
Eric Newberryb5aa7f52016-09-03 20:36:12 -0700362}
363
Davide Pesavento28181322018-11-08 16:44:50 -0500364static ndn::nfd::FaceStatus
Davide Pesavento412c9822021-07-02 00:21:05 -0400365makeFaceStatus(const Face& face, const time::steady_clock::time_point& now)
Yanbiao Li58ba3f92017-02-15 14:27:18 +0000366{
Davide Pesavento28181322018-11-08 16:44:50 -0500367 ndn::nfd::FaceStatus status;
368 copyFaceProperties(face, status);
369
370 auto expirationTime = face.getExpirationTime();
Davide Pesavento412c9822021-07-02 00:21:05 -0400371 if (expirationTime != time::steady_clock::time_point::max()) {
Davide Pesavento28181322018-11-08 16:44:50 -0500372 status.setExpirationPeriod(std::max(0_ms,
373 time::duration_cast<time::milliseconds>(expirationTime - now)));
374 }
375
Yanbiao Li58ba3f92017-02-15 14:27:18 +0000376 auto linkService = dynamic_cast<face::GenericLinkService*>(face.getLinkService());
Davide Pesavento28181322018-11-08 16:44:50 -0500377 if (linkService != nullptr) {
378 const auto& options = linkService->getOptions();
379 status.setBaseCongestionMarkingInterval(options.baseCongestionMarkingInterval)
380 .setDefaultCongestionThreshold(options.defaultCongestionThreshold);
Eric Newberry812d6152018-06-06 15:06:01 -0700381 }
382
Davide Pesavento28181322018-11-08 16:44:50 -0500383 copyMtu(face, status);
384
385 const auto& counters = face.getCounters();
386 status.setNInInterests(counters.nInInterests)
387 .setNOutInterests(counters.nOutInterests)
388 .setNInData(counters.nInData)
389 .setNOutData(counters.nOutData)
390 .setNInNacks(counters.nInNacks)
391 .setNOutNacks(counters.nOutNacks)
392 .setNInBytes(counters.nInBytes)
393 .setNOutBytes(counters.nOutBytes);
394
395 return status;
Yanbiao Li58ba3f92017-02-15 14:27:18 +0000396}
397
Eric Newberryb5aa7f52016-09-03 20:36:12 -0700398void
Davide Pesavento28181322018-11-08 16:44:50 -0500399FaceManager::listFaces(ndn::mgmt::StatusDatasetContext& context)
Yanbiao Li73860e32015-08-19 16:30:16 -0700400{
Eric Newberryc64d30a2015-12-26 11:07:27 -0700401 auto now = time::steady_clock::now();
Davide Pesavento28181322018-11-08 16:44:50 -0500402 for (const auto& face : m_faceTable) {
403 ndn::nfd::FaceStatus status = makeFaceStatus(face, now);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700404 context.append(status.wireEncode());
Yanbiao Li73860e32015-08-19 16:30:16 -0700405 }
406 context.end();
407}
408
409void
Davide Pesavento28181322018-11-08 16:44:50 -0500410FaceManager::listChannels(ndn::mgmt::StatusDatasetContext& context)
Yanbiao Li73860e32015-08-19 16:30:16 -0700411{
Davide Pesavento28181322018-11-08 16:44:50 -0500412 auto factories = m_faceSystem.listProtocolFactories();
Davide Pesaventoe5eebad2017-04-06 20:23:26 -0400413 for (const auto* factory : factories) {
Junxiao Shib8590312016-12-29 21:22:25 +0000414 for (const auto& channel : factory->getChannels()) {
415 ndn::nfd::ChannelStatus entry;
416 entry.setLocalUri(channel->getUri().toString());
417 context.append(entry.wireEncode());
Yanbiao Li73860e32015-08-19 16:30:16 -0700418 }
419 }
Yanbiao Li73860e32015-08-19 16:30:16 -0700420 context.end();
421}
422
Davide Pesavento28181322018-11-08 16:44:50 -0500423static bool
424matchFilter(const ndn::nfd::FaceQueryFilter& filter, const Face& face)
Yanbiao Li73860e32015-08-19 16:30:16 -0700425{
426 if (filter.hasFaceId() &&
Junxiao Shib84e6742016-07-19 13:16:22 +0000427 filter.getFaceId() != static_cast<uint64_t>(face.getId())) {
Yanbiao Li73860e32015-08-19 16:30:16 -0700428 return false;
429 }
430
431 if (filter.hasUriScheme() &&
Junxiao Shib84e6742016-07-19 13:16:22 +0000432 filter.getUriScheme() != face.getRemoteUri().getScheme() &&
433 filter.getUriScheme() != face.getLocalUri().getScheme()) {
Yanbiao Li73860e32015-08-19 16:30:16 -0700434 return false;
435 }
436
437 if (filter.hasRemoteUri() &&
Junxiao Shib84e6742016-07-19 13:16:22 +0000438 filter.getRemoteUri() != face.getRemoteUri().toString()) {
Yanbiao Li73860e32015-08-19 16:30:16 -0700439 return false;
440 }
441
442 if (filter.hasLocalUri() &&
Junxiao Shib84e6742016-07-19 13:16:22 +0000443 filter.getLocalUri() != face.getLocalUri().toString()) {
Yanbiao Li73860e32015-08-19 16:30:16 -0700444 return false;
445 }
446
447 if (filter.hasFaceScope() &&
Junxiao Shib84e6742016-07-19 13:16:22 +0000448 filter.getFaceScope() != face.getScope()) {
Yanbiao Li73860e32015-08-19 16:30:16 -0700449 return false;
450 }
451
452 if (filter.hasFacePersistency() &&
Junxiao Shib84e6742016-07-19 13:16:22 +0000453 filter.getFacePersistency() != face.getPersistency()) {
Yanbiao Li73860e32015-08-19 16:30:16 -0700454 return false;
455 }
456
457 if (filter.hasLinkType() &&
Junxiao Shib84e6742016-07-19 13:16:22 +0000458 filter.getLinkType() != face.getLinkType()) {
Yanbiao Li73860e32015-08-19 16:30:16 -0700459 return false;
460 }
461
462 return true;
463}
464
Junxiao Shicde37ad2015-12-24 01:02:05 -0700465void
Davide Pesavento28181322018-11-08 16:44:50 -0500466FaceManager::queryFaces(const Interest& interest,
467 ndn::mgmt::StatusDatasetContext& context)
Junxiao Shida93f1f2015-11-11 06:13:16 -0700468{
Davide Pesavento28181322018-11-08 16:44:50 -0500469 ndn::nfd::FaceQueryFilter faceFilter;
470 try {
471 faceFilter.wireDecode(interest.getName()[-1].blockFromValue());
Eric Newberry1b4ba052016-10-07 23:04:07 -0700472 }
Davide Pesavento28181322018-11-08 16:44:50 -0500473 catch (const tlv::Error& e) {
474 NFD_LOG_DEBUG("Malformed query filter: " << e.what());
475 return context.reject(ControlResponse(400, "Malformed filter"));
476 }
477
478 auto now = time::steady_clock::now();
479 for (const auto& face : m_faceTable) {
480 if (matchFilter(faceFilter, face)) {
481 ndn::nfd::FaceStatus status = makeFaceStatus(face, now);
482 context.append(status.wireEncode());
483 }
484 }
485 context.end();
Junxiao Shida93f1f2015-11-11 06:13:16 -0700486}
487
488void
Eric Newberry1b4ba052016-10-07 23:04:07 -0700489FaceManager::notifyFaceEvent(const Face& face, ndn::nfd::FaceEventKind kind)
Yanbiao Li73860e32015-08-19 16:30:16 -0700490{
491 ndn::nfd::FaceEventNotification notification;
Eric Newberry1b4ba052016-10-07 23:04:07 -0700492 notification.setKind(kind);
Davide Pesavento28181322018-11-08 16:44:50 -0500493 copyFaceProperties(face, notification);
Yanbiao Li73860e32015-08-19 16:30:16 -0700494
Junxiao Shiae04d342016-07-19 13:20:22 +0000495 m_postNotification(notification.wireEncode());
Yanbiao Li73860e32015-08-19 16:30:16 -0700496}
497
498void
Eric Newberry1b4ba052016-10-07 23:04:07 -0700499FaceManager::connectFaceStateChangeSignal(const Face& face)
Yanbiao Li73860e32015-08-19 16:30:16 -0700500{
Davide Pesavento3cf75dc2018-03-17 00:38:03 -0400501 using face::FaceState;
502
Eric Newberry1b4ba052016-10-07 23:04:07 -0700503 FaceId faceId = face.getId();
504 m_faceStateChangeConn[faceId] = face.afterStateChange.connect(
Davide Pesavento3cf75dc2018-03-17 00:38:03 -0400505 [this, faceId, &face] (FaceState oldState, FaceState newState) {
506 if (newState == FaceState::UP) {
Eric Newberry1b4ba052016-10-07 23:04:07 -0700507 notifyFaceEvent(face, ndn::nfd::FACE_EVENT_UP);
508 }
Davide Pesavento3cf75dc2018-03-17 00:38:03 -0400509 else if (newState == FaceState::DOWN) {
Eric Newberry1b4ba052016-10-07 23:04:07 -0700510 notifyFaceEvent(face, ndn::nfd::FACE_EVENT_DOWN);
511 }
Davide Pesavento3cf75dc2018-03-17 00:38:03 -0400512 else if (newState == FaceState::CLOSED) {
513 // cannot use face.getId() because it may already be reset to INVALID_FACEID
Eric Newberry1b4ba052016-10-07 23:04:07 -0700514 m_faceStateChangeConn.erase(faceId);
515 }
516 });
Yanbiao Li73860e32015-08-19 16:30:16 -0700517}
518
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200519} // namespace nfd