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>
48 , m_faceSystem(faceSystem)
49 , m_faceTable(faceSystem.getFaceTable())
52 registerCommandHandler<ndn::nfd::FaceCreateCommand>([
this] (
auto&&,
auto&&,
auto&&... args) {
53 createFace(std::forward<decltype(args)>(args)...);
55 registerCommandHandler<ndn::nfd::FaceUpdateCommand>([
this] (
auto&&,
auto&&... args) {
56 updateFace(std::forward<decltype(args)>(args)...);
58 registerCommandHandler<ndn::nfd::FaceDestroyCommand>([
this] (
auto&&,
auto&&,
auto&&... args) {
59 destroyFace(std::forward<decltype(args)>(args)...);
64 [
this] (
auto&&,
auto&&,
auto&&... args) { listFaces(std::forward<decltype(args)>(args)...); });
66 [
this] (
auto&&,
auto&&,
auto&&... args) { listChannels(std::forward<decltype(args)>(args)...); });
68 [
this] (
auto&&,
auto&&... args) { queryFaces(std::forward<decltype(args)>(args)...); });
72 m_faceAddConn = m_faceTable.
afterAdd.connect([
this] (
const Face& face) {
73 connectFaceStateChangeSignal(face);
74 notifyFaceEvent(face, ndn::nfd::FACE_EVENT_CREATED);
76 m_faceRemoveConn = m_faceTable.
beforeRemove.connect([
this] (
const Face& face) {
77 notifyFaceEvent(face, ndn::nfd::FACE_EVENT_DESTROYED);
82 FaceManager::createFace(
const ControlParameters& parameters,
83 const CommandContinuation& done)
86 if (!remoteUri.parse(parameters.getUri())) {
87 NFD_LOG_TRACE(
"failed to parse remote URI: " << parameters.getUri());
88 done(ControlResponse(400,
"Malformed command"));
92 if (!remoteUri.isCanonical()) {
93 NFD_LOG_TRACE(
"received non-canonical remote URI: " << remoteUri.toString());
94 done(ControlResponse(400,
"Non-canonical remote URI"));
98 std::optional<FaceUri> localUri;
99 if (parameters.hasLocalUri()) {
100 localUri = FaceUri{};
102 if (!localUri->parse(parameters.getLocalUri())) {
103 NFD_LOG_TRACE(
"failed to parse local URI: " << parameters.getLocalUri());
104 done(ControlResponse(400,
"Malformed command"));
108 if (!localUri->isCanonical()) {
109 NFD_LOG_TRACE(
"received non-canonical local URI: " << localUri->toString());
110 done(ControlResponse(400,
"Non-canonical local URI"));
115 face::ProtocolFactory* factory = m_faceSystem.
getFactoryByScheme(remoteUri.getScheme());
116 if (factory ==
nullptr) {
117 NFD_LOG_TRACE(
"received create request for unsupported protocol: " << remoteUri.getScheme());
118 done(ControlResponse(406,
"Unsupported protocol"));
122 face::FaceParams faceParams;
123 faceParams.persistency = parameters.getFacePersistency();
124 if (parameters.hasBaseCongestionMarkingInterval()) {
125 faceParams.baseCongestionMarkingInterval = parameters.getBaseCongestionMarkingInterval();
127 if (parameters.hasDefaultCongestionThreshold()) {
128 faceParams.defaultCongestionThreshold = parameters.getDefaultCongestionThreshold();
130 if (parameters.hasMtu()) {
132 faceParams.mtu = std::min<uint64_t>(std::numeric_limits<ssize_t>::max(), parameters.getMtu());
134 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);
138 if (parameters.hasFlagBit(ndn::nfd::BIT_CONGESTION_MARKING_ENABLED)) {
139 faceParams.wantCongestionMarking = parameters.getFlagBit(ndn::nfd::BIT_CONGESTION_MARKING_ENABLED);
142 factory->createFace({remoteUri, localUri, faceParams},
143 [
this, parameters, done] (
const auto& face) {
144 this->afterCreateFaceSuccess(face, parameters, done);
146 [done] (uint32_t status,
const std::string& reason) {
148 done(ControlResponse(status, reason));
151 catch (
const std::runtime_error& error) {
153 done(ControlResponse(500,
"Face creation failed due to internal error"));
156 catch (
const std::logic_error& error) {
158 done(ControlResponse(500,
"Face creation failed due to internal error"));
168 to.setMtu(std::min<size_t>(face.
getMtu(), ndn::MAX_NDN_PACKET_SIZE));
171 to.setMtu(ndn::MAX_NDN_PACKET_SIZE);
175 static ControlParameters
178 ControlParameters params;
179 params.setFaceId(face.
getId())
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);
196 static ControlParameters
207 FaceManager::afterCreateFaceSuccess(
const shared_ptr<Face>& face,
208 const ControlParameters& parameters,
209 const CommandContinuation& done)
212 NFD_LOG_TRACE(
"Attempted to create duplicate face of " << face->getId());
214 done(ControlResponse(409,
"Face with remote URI already exists").setBody(response.wireEncode()));
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)));
225 m_faceTable.
add(face);
228 done(ControlResponse(200,
"OK").setBody(response.wireEncode()));
235 if (linkService ==
nullptr) {
240 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);
244 if (parameters.hasFlagBit(ndn::nfd::BIT_LP_RELIABILITY_ENABLED)) {
245 options.reliabilityOptions.isEnabled = parameters.getFlagBit(ndn::nfd::BIT_LP_RELIABILITY_ENABLED);
247 if (parameters.hasFlagBit(ndn::nfd::BIT_CONGESTION_MARKING_ENABLED)) {
248 options.allowCongestionMarking = parameters.getFlagBit(ndn::nfd::BIT_CONGESTION_MARKING_ENABLED);
250 if (parameters.hasBaseCongestionMarkingInterval()) {
251 options.baseCongestionMarkingInterval = parameters.getBaseCongestionMarkingInterval();
253 if (parameters.hasDefaultCongestionThreshold()) {
254 options.defaultCongestionThreshold = parameters.getDefaultCongestionThreshold();
257 if (parameters.hasMtu()) {
259 options.overrideMtu = std::min<uint64_t>(std::numeric_limits<ssize_t>::max(), parameters.getMtu());
262 linkService->setOptions(options);
266 FaceManager::updateFace(
const Interest& interest,
267 const ControlParameters& parameters,
268 const CommandContinuation& done)
270 FaceId faceId = parameters.getFaceId();
272 auto incomingFaceIdTag = interest.getTag<lp::IncomingFaceIdTag>();
273 if (incomingFaceIdTag ==
nullptr) {
275 done(ControlResponse(404,
"No FaceId specified and IncomingFaceId not available"));
278 faceId = *incomingFaceIdTag;
281 Face* face = m_faceTable.
get(faceId);
282 if (face ==
nullptr) {
284 done(ControlResponse(404,
"Specified face does not exist"));
289 ControlParameters response;
290 bool areParamsValid =
true;
292 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));
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);
312 if (parameters.hasMtu()) {
313 auto mtu = parameters.getMtu();
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)) {
319 areParamsValid =
false;
320 response.setMtu(mtu);
324 if (!areParamsValid) {
325 done(ControlResponse(409,
"Invalid properties specified").setBody(response.wireEncode()));
330 if (parameters.hasFacePersistency()) {
331 face->setPersistency(parameters.getFacePersistency());
337 done(ControlResponse(200,
"OK").setBody(response.wireEncode()));
341 FaceManager::destroyFace(
const ControlParameters& parameters,
342 const CommandContinuation& done)
344 Face* face = m_faceTable.
get(parameters.getFaceId());
345 if (face !=
nullptr) {
349 done(ControlResponse(200,
"OK").setBody(parameters.wireEncode()));
356 to.setFaceId(face.
getId())
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);
372 static ndn::nfd::FaceStatus
375 ndn::nfd::FaceStatus status;
379 if (expirationTime != time::steady_clock::time_point::max()) {
380 status.setExpirationPeriod(std::max(0_ms,
381 time::duration_cast<time::milliseconds>(expirationTime - now)));
385 if (linkService !=
nullptr) {
386 const auto& options = linkService->
getOptions();
387 status.setBaseCongestionMarkingInterval(options.baseCongestionMarkingInterval)
388 .setDefaultCongestionThreshold(options.defaultCongestionThreshold);
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);
407 FaceManager::listFaces(ndn::mgmt::StatusDatasetContext& context)
409 auto now = time::steady_clock::now();
410 for (
const auto& face : m_faceTable) {
412 context.append(status.wireEncode());
418 FaceManager::listChannels(ndn::mgmt::StatusDatasetContext& context)
421 for (
const auto* factory : factories) {
422 for (
const auto& channel : factory->getChannels()) {
423 ndn::nfd::ChannelStatus entry;
424 entry.setLocalUri(channel->getUri().toString());
425 context.append(entry.wireEncode());
434 if (filter.hasFaceId() &&
435 filter.getFaceId() !=
static_cast<uint64_t
>(face.
getId())) {
439 if (filter.hasUriScheme() &&
440 filter.getUriScheme() != face.
getRemoteUri().getScheme() &&
441 filter.getUriScheme() != face.
getLocalUri().getScheme()) {
445 if (filter.hasRemoteUri() &&
446 filter.getRemoteUri() != face.
getRemoteUri().toString()) {
450 if (filter.hasLocalUri() &&
451 filter.getLocalUri() != face.
getLocalUri().toString()) {
455 if (filter.hasFaceScope() &&
456 filter.getFaceScope() != face.
getScope()) {
460 if (filter.hasFacePersistency() &&
465 if (filter.hasLinkType() &&
474 FaceManager::queryFaces(
const Interest& interest,
475 ndn::mgmt::StatusDatasetContext& context)
477 ndn::nfd::FaceQueryFilter faceFilter;
479 faceFilter.wireDecode(interest.getName()[-1].blockFromValue());
481 catch (
const tlv::Error& e) {
483 return context.reject(ControlResponse(400,
"Malformed filter"));
486 auto now = time::steady_clock::now();
487 for (
const auto& face : m_faceTable) {
490 context.append(status.wireEncode());
497 FaceManager::notifyFaceEvent(
const Face& face, ndn::nfd::FaceEventKind kind)
499 ndn::nfd::FaceEventNotification notification;
500 notification.setKind(kind);
503 m_postNotification(notification.wireEncode());
507 FaceManager::connectFaceStateChangeSignal(
const Face& face)
511 FaceId faceId = face.getId();
512 m_faceStateChangeConn[faceId] = face.afterStateChange.connect(
514 if (newState == FaceState::UP) {
515 notifyFaceEvent(face, ndn::nfd::FACE_EVENT_UP);
517 else if (newState == FaceState::DOWN) {
518 notifyFaceEvent(face, ndn::nfd::FACE_EVENT_DOWN);
520 else if (newState == FaceState::CLOSED) {
522 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.
ndn::nfd::FaceScope getScope() const noexcept
Returns whether the face is local or non-local for scope control purposes.
FaceUri getLocalUri() const noexcept
Returns a FaceUri representing the local endpoint.
ndn::nfd::LinkType getLinkType() const noexcept
Returns the link type of the face (point-to-point, multi-access, ...).
FaceUri getRemoteUri() const noexcept
Returns a FaceUri representing the remote endpoint.
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.
const FaceCounters & getCounters() const noexcept
ndn::nfd::FacePersistency getPersistency() const noexcept
Returns the current persistency setting of the face.
time::steady_clock::time_point getExpirationTime() const noexcept
Returns the expiration time of the face.
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) const
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)
bool allowLocalFields
Enables encoding of IncomingFaceId, and decoding of NextHopFaceId and CachePolicy.