33 #include <ndn-cxx/lp/tags.hpp>
34 #include <ndn-cxx/mgmt/nfd/channel-status.hpp>
35 #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>
46 , m_faceSystem(faceSystem)
47 , m_faceTable(faceSystem.getFaceTable())
50 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));
64 m_faceAddConn = m_faceTable.
afterAdd.connect([
this] (
const Face& face) {
65 connectFaceStateChangeSignal(face);
66 notifyFaceEvent(face, ndn::nfd::FACE_EVENT_CREATED);
68 m_faceRemoveConn = m_faceTable.
beforeRemove.connect([
this] (
const Face& face) {
69 notifyFaceEvent(face, ndn::nfd::FACE_EVENT_DESTROYED);
74 FaceManager::createFace(
const ControlParameters& parameters,
75 const ndn::mgmt::CommandContinuation& done)
78 if (!remoteUri.parse(parameters.getUri())) {
79 NFD_LOG_TRACE(
"failed to parse remote URI: " << parameters.getUri());
80 done(ControlResponse(400,
"Malformed command"));
84 if (!remoteUri.isCanonical()) {
85 NFD_LOG_TRACE(
"received non-canonical remote URI: " << remoteUri.toString());
86 done(ControlResponse(400,
"Non-canonical remote URI"));
90 std::optional<FaceUri> localUri;
91 if (parameters.hasLocalUri()) {
94 if (!localUri->parse(parameters.getLocalUri())) {
95 NFD_LOG_TRACE(
"failed to parse local URI: " << parameters.getLocalUri());
96 done(ControlResponse(400,
"Malformed command"));
100 if (!localUri->isCanonical()) {
101 NFD_LOG_TRACE(
"received non-canonical local URI: " << localUri->toString());
102 done(ControlResponse(400,
"Non-canonical local URI"));
107 face::ProtocolFactory* factory = m_faceSystem.
getFactoryByScheme(remoteUri.getScheme());
108 if (factory ==
nullptr) {
109 NFD_LOG_TRACE(
"received create request for unsupported protocol: " << remoteUri.getScheme());
110 done(ControlResponse(406,
"Unsupported protocol"));
114 face::FaceParams faceParams;
115 faceParams.persistency = parameters.getFacePersistency();
116 if (parameters.hasBaseCongestionMarkingInterval()) {
117 faceParams.baseCongestionMarkingInterval = parameters.getBaseCongestionMarkingInterval();
119 if (parameters.hasDefaultCongestionThreshold()) {
120 faceParams.defaultCongestionThreshold = parameters.getDefaultCongestionThreshold();
122 if (parameters.hasMtu()) {
124 faceParams.mtu = std::min<uint64_t>(std::numeric_limits<ssize_t>::max(), parameters.getMtu());
126 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);
130 if (parameters.hasFlagBit(ndn::nfd::BIT_CONGESTION_MARKING_ENABLED)) {
131 faceParams.wantCongestionMarking = parameters.getFlagBit(ndn::nfd::BIT_CONGESTION_MARKING_ENABLED);
134 factory->createFace({remoteUri, localUri, faceParams},
135 [
this, parameters, done] (
const auto& face) {
136 this->afterCreateFaceSuccess(face, parameters, done);
138 [done] (uint32_t status,
const std::string& reason) {
140 done(ControlResponse(status, reason));
143 catch (
const std::runtime_error& error) {
145 done(ControlResponse(500,
"Face creation failed due to internal error"));
148 catch (
const std::logic_error& error) {
150 done(ControlResponse(500,
"Face creation failed due to internal error"));
160 to.setMtu(std::min<size_t>(face.
getMtu(), ndn::MAX_NDN_PACKET_SIZE));
163 to.setMtu(ndn::MAX_NDN_PACKET_SIZE);
167 static ControlParameters
170 ControlParameters params;
171 params.setFaceId(face.
getId())
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);
188 static ControlParameters
199 FaceManager::afterCreateFaceSuccess(
const shared_ptr<Face>& face,
200 const ControlParameters& parameters,
201 const ndn::mgmt::CommandContinuation& done)
204 NFD_LOG_TRACE(
"Attempted to create duplicate face of " << face->getId());
206 done(ControlResponse(409,
"Face with remote URI already exists").setBody(response.wireEncode()));
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)));
217 m_faceTable.
add(face);
220 done(ControlResponse(200,
"OK").setBody(response.wireEncode()));
227 if (linkService ==
nullptr) {
232 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);
236 if (parameters.hasFlagBit(ndn::nfd::BIT_LP_RELIABILITY_ENABLED)) {
237 options.reliabilityOptions.isEnabled = parameters.getFlagBit(ndn::nfd::BIT_LP_RELIABILITY_ENABLED);
239 if (parameters.hasFlagBit(ndn::nfd::BIT_CONGESTION_MARKING_ENABLED)) {
240 options.allowCongestionMarking = parameters.getFlagBit(ndn::nfd::BIT_CONGESTION_MARKING_ENABLED);
242 if (parameters.hasBaseCongestionMarkingInterval()) {
243 options.baseCongestionMarkingInterval = parameters.getBaseCongestionMarkingInterval();
245 if (parameters.hasDefaultCongestionThreshold()) {
246 options.defaultCongestionThreshold = parameters.getDefaultCongestionThreshold();
249 if (parameters.hasMtu()) {
251 options.overrideMtu = std::min<uint64_t>(std::numeric_limits<ssize_t>::max(), parameters.getMtu());
254 linkService->setOptions(options);
258 FaceManager::updateFace(
const Interest& interest,
259 const ControlParameters& parameters,
260 const ndn::mgmt::CommandContinuation& done)
262 FaceId faceId = parameters.getFaceId();
264 auto incomingFaceIdTag = interest.getTag<lp::IncomingFaceIdTag>();
265 if (incomingFaceIdTag ==
nullptr) {
267 done(ControlResponse(404,
"No FaceId specified and IncomingFaceId not available"));
270 faceId = *incomingFaceIdTag;
273 Face* face = m_faceTable.
get(faceId);
274 if (face ==
nullptr) {
276 done(ControlResponse(404,
"Specified face does not exist"));
281 ControlParameters response;
282 bool areParamsValid =
true;
284 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));
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);
304 if (parameters.hasMtu()) {
305 auto mtu = parameters.getMtu();
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)) {
311 areParamsValid =
false;
312 response.setMtu(mtu);
316 if (!areParamsValid) {
317 done(ControlResponse(409,
"Invalid properties specified").setBody(response.wireEncode()));
322 if (parameters.hasFacePersistency()) {
323 face->setPersistency(parameters.getFacePersistency());
329 done(ControlResponse(200,
"OK").setBody(response.wireEncode()));
333 FaceManager::destroyFace(
const ControlParameters& parameters,
334 const ndn::mgmt::CommandContinuation& done)
336 Face* face = m_faceTable.
get(parameters.getFaceId());
337 if (face !=
nullptr) {
341 done(ControlResponse(200,
"OK").setBody(parameters.wireEncode()));
348 to.setFaceId(face.
getId())
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);
364 static ndn::nfd::FaceStatus
367 ndn::nfd::FaceStatus status;
371 if (expirationTime != time::steady_clock::time_point::max()) {
372 status.setExpirationPeriod(std::max(0_ms,
373 time::duration_cast<time::milliseconds>(expirationTime - now)));
377 if (linkService !=
nullptr) {
378 const auto& options = linkService->
getOptions();
379 status.setBaseCongestionMarkingInterval(options.baseCongestionMarkingInterval)
380 .setDefaultCongestionThreshold(options.defaultCongestionThreshold);
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);
399 FaceManager::listFaces(ndn::mgmt::StatusDatasetContext& context)
401 auto now = time::steady_clock::now();
402 for (
const auto& face : m_faceTable) {
404 context.append(status.wireEncode());
410 FaceManager::listChannels(ndn::mgmt::StatusDatasetContext& context)
413 for (
const auto* factory : factories) {
414 for (
const auto& channel : factory->getChannels()) {
415 ndn::nfd::ChannelStatus entry;
416 entry.setLocalUri(channel->getUri().toString());
417 context.append(entry.wireEncode());
426 if (filter.hasFaceId() &&
427 filter.getFaceId() !=
static_cast<uint64_t
>(face.
getId())) {
431 if (filter.hasUriScheme() &&
432 filter.getUriScheme() != face.
getRemoteUri().getScheme() &&
433 filter.getUriScheme() != face.
getLocalUri().getScheme()) {
437 if (filter.hasRemoteUri() &&
438 filter.getRemoteUri() != face.
getRemoteUri().toString()) {
442 if (filter.hasLocalUri() &&
443 filter.getLocalUri() != face.
getLocalUri().toString()) {
447 if (filter.hasFaceScope() &&
448 filter.getFaceScope() != face.
getScope()) {
452 if (filter.hasFacePersistency() &&
457 if (filter.hasLinkType() &&
466 FaceManager::queryFaces(
const Interest& interest,
467 ndn::mgmt::StatusDatasetContext& context)
469 ndn::nfd::FaceQueryFilter faceFilter;
471 faceFilter.wireDecode(interest.getName()[-1].blockFromValue());
473 catch (
const tlv::Error& e) {
475 return context.reject(ControlResponse(400,
"Malformed filter"));
478 auto now = time::steady_clock::now();
479 for (
const auto& face : m_faceTable) {
482 context.append(status.wireEncode());
489 FaceManager::notifyFaceEvent(
const Face& face, ndn::nfd::FaceEventKind kind)
491 ndn::nfd::FaceEventNotification notification;
492 notification.setKind(kind);
495 m_postNotification(notification.wireEncode());
499 FaceManager::connectFaceStateChangeSignal(
const Face& face)
503 FaceId faceId = face.getId();
504 m_faceStateChangeConn[faceId] = face.afterStateChange.connect(
506 if (newState == FaceState::UP) {
507 notifyFaceEvent(face, ndn::nfd::FACE_EVENT_UP);
509 else if (newState == FaceState::DOWN) {
510 notifyFaceEvent(face, ndn::nfd::FACE_EVENT_DOWN);
512 else if (newState == FaceState::CLOSED) {
514 m_faceStateChangeConn.erase(faceId);
Provides ControlCommand authorization according to NFD's configuration file.
FaceManager(FaceSystem &faceSystem, Dispatcher &dispatcher, CommandAuthenticator &authenticator)
signal::Signal< FaceTable, Face > beforeRemove
Fires immediately before a face is removed.
signal::Signal< FaceTable, Face > afterAdd
Fires immediately after a face is added.
Face * get(FaceId id) const noexcept
Get face by FaceId.
void add(shared_ptr< Face > face)
Add a face.
A collection of common functions shared by all NFD managers, such as communicating with the dispatche...
ndn::mgmt::PostNotification registerNotificationStream(const std::string &verb)
void registerStatusDatasetHandler(const std::string &verb, const ndn::mgmt::StatusDatasetHandler &handler)
Generalization of a network interface.
time::steady_clock::time_point getExpirationTime() const
Returns the expiration time of the face.
ndn::nfd::FaceScope getScope() const
Returns whether the face is local or non-local for scope control purposes.
FaceUri getRemoteUri() const
Returns a FaceUri representing the remote endpoint.
ndn::nfd::FacePersistency getPersistency() const
Returns the current persistency setting of the face.
ssize_t getMtu() const
Returns the effective MTU of the face.
LinkService * getLinkService() const noexcept
FaceId getId() const noexcept
Returns the face ID.
void close()
Request that the face be closed.
FaceUri getLocalUri() const
Returns a FaceUri representing the local endpoint.
const FaceCounters & getCounters() const noexcept
ndn::nfd::LinkType getLinkType() const
Returns the link type of the face (point-to-point, multi-access, ...).
Entry point of NFD's face system.
std::set< const ProtocolFactory * > listProtocolFactories() const
Returns all ProtocolFactory objects owned by the face system.
ProtocolFactory * getFactoryByScheme(const std::string &scheme)
bool allowLocalFields
Enables encoding of IncomingFaceId, and decoding of NextHopFaceId and CachePolicy.
GenericLinkService is a LinkService that implements the NDNLPv2 protocol.
const Options & getOptions() const
Get the options used by GenericLinkService.
#define NFD_LOG_INIT(name)
TransportState FaceState
Indicates the state of a face.
constexpr FaceId INVALID_FACEID
Indicates an invalid FaceId.
constexpr ssize_t MTU_UNLIMITED
Indicates that the transport has no limit on payload size.
uint64_t FaceId
Identifies a face.
static void copyFaceProperties(const Face &face, T &to)
static ControlParameters makeUpdateFaceResponse(const Face &face)
static void updateLinkServiceOptions(Face &face, const ControlParameters ¶meters)
static bool matchFilter(const ndn::nfd::FaceQueryFilter &filter, const Face &face)
static ControlParameters makeCreateFaceResponse(const Face &face)
static void copyMtu(const Face &face, T &to)
static ndn::nfd::FaceStatus makeFaceStatus(const Face &face, const time::steady_clock::time_point &now)