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", bind(&FaceManager::createFace,
this, _4, _5));
51 registerCommandHandler<ndn::nfd::FaceUpdateCommand>(
"update", bind(&FaceManager::updateFace,
this, _3, _4, _5));
52 registerCommandHandler<ndn::nfd::FaceDestroyCommand>(
"destroy", bind(&FaceManager::destroyFace,
this, _4, _5));
61 m_faceAddConn = m_faceTable.
afterAdd.connect([
this] (
const Face& face) {
62 connectFaceStateChangeSignal(face);
63 notifyFaceEvent(face, ndn::nfd::FACE_EVENT_CREATED);
65 m_faceRemoveConn = m_faceTable.
beforeRemove.connect([
this] (
const Face& face) {
66 notifyFaceEvent(face, ndn::nfd::FACE_EVENT_DESTROYED);
71 FaceManager::createFace(
const ControlParameters& parameters,
72 const ndn::mgmt::CommandContinuation& done)
75 if (!remoteUri.parse(parameters.getUri())) {
76 NFD_LOG_TRACE(
"failed to parse remote URI: " << parameters.getUri());
77 done(ControlResponse(400,
"Malformed command"));
81 if (!remoteUri.isCanonical()) {
82 NFD_LOG_TRACE(
"received non-canonical remote URI: " << remoteUri.toString());
83 done(ControlResponse(400,
"Non-canonical remote URI"));
87 optional<FaceUri> localUri;
88 if (parameters.hasLocalUri()) {
91 if (!localUri->parse(parameters.getLocalUri())) {
92 NFD_LOG_TRACE(
"failed to parse local URI: " << parameters.getLocalUri());
93 done(ControlResponse(400,
"Malformed command"));
97 if (!localUri->isCanonical()) {
98 NFD_LOG_TRACE(
"received non-canonical local URI: " << localUri->toString());
99 done(ControlResponse(400,
"Non-canonical local URI"));
105 if (factory ==
nullptr) {
106 NFD_LOG_TRACE(
"received create request for unsupported protocol: " << remoteUri.getScheme());
107 done(ControlResponse(406,
"Unsupported protocol"));
112 faceParams.
persistency = parameters.getFacePersistency();
113 if (parameters.hasBaseCongestionMarkingInterval()) {
116 if (parameters.hasDefaultCongestionThreshold()) {
119 if (parameters.hasMtu()) {
121 faceParams.
mtu = std::min<uint64_t>(std::numeric_limits<ssize_t>::max(), parameters.getMtu());
123 faceParams.
wantLocalFields = parameters.hasFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED) &&
124 parameters.getFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED);
125 faceParams.
wantLpReliability = parameters.hasFlagBit(ndn::nfd::BIT_LP_RELIABILITY_ENABLED) &&
126 parameters.getFlagBit(ndn::nfd::BIT_LP_RELIABILITY_ENABLED);
127 if (parameters.hasFlagBit(ndn::nfd::BIT_CONGESTION_MARKING_ENABLED)) {
131 factory->
createFace({remoteUri, localUri, faceParams},
132 [
this, parameters, done] (
const auto& face) {
133 this->afterCreateFaceSuccess(face, parameters, done);
135 [done] (uint32_t status,
const std::string& reason) {
137 done(ControlResponse(status, reason));
140 catch (
const std::runtime_error& error) {
142 done(ControlResponse(500,
"Face creation failed due to internal error"));
145 catch (
const std::logic_error& error) {
147 done(ControlResponse(500,
"Face creation failed due to internal error"));
157 to.setMtu(std::min<size_t>(face.
getMtu(), ndn::MAX_NDN_PACKET_SIZE));
160 to.setMtu(ndn::MAX_NDN_PACKET_SIZE);
164 static ControlParameters
167 ControlParameters params;
168 params.setFaceId(face.
getId())
173 if (linkService !=
nullptr) {
174 const auto& options = linkService->
getOptions();
175 params.setBaseCongestionMarkingInterval(options.baseCongestionMarkingInterval)
176 .setDefaultCongestionThreshold(options.defaultCongestionThreshold)
177 .setFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED, options.allowLocalFields,
false)
178 .setFlagBit(ndn::nfd::BIT_LP_RELIABILITY_ENABLED, options.reliabilityOptions.isEnabled,
false)
179 .setFlagBit(ndn::nfd::BIT_CONGESTION_MARKING_ENABLED, options.allowCongestionMarking,
false);
185 static ControlParameters
196 FaceManager::afterCreateFaceSuccess(
const shared_ptr<Face>& face,
197 const ControlParameters& parameters,
198 const ndn::mgmt::CommandContinuation& done)
201 NFD_LOG_TRACE(
"Attempted to create duplicate face of " << face->getId());
203 done(ControlResponse(409,
"Face with remote URI already exists").setBody(response.wireEncode()));
209 BOOST_ASSERT(face->getScope() == ndn::nfd::FACE_SCOPE_LOCAL ||
210 !parameters.hasFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED) ||
211 (parameters.hasFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED) &&
212 !parameters.getFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED)));
214 m_faceTable.
add(face);
217 done(ControlResponse(200,
"OK").setBody(response.wireEncode()));
224 if (linkService ==
nullptr) {
229 if (parameters.hasFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED) &&
230 face.
getScope() == ndn::nfd::FACE_SCOPE_LOCAL) {
231 options.
allowLocalFields = parameters.getFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED);
233 if (parameters.hasFlagBit(ndn::nfd::BIT_LP_RELIABILITY_ENABLED)) {
234 options.reliabilityOptions.isEnabled = parameters.getFlagBit(ndn::nfd::BIT_LP_RELIABILITY_ENABLED);
236 if (parameters.hasFlagBit(ndn::nfd::BIT_CONGESTION_MARKING_ENABLED)) {
237 options.allowCongestionMarking = parameters.getFlagBit(ndn::nfd::BIT_CONGESTION_MARKING_ENABLED);
239 if (parameters.hasBaseCongestionMarkingInterval()) {
240 options.baseCongestionMarkingInterval = parameters.getBaseCongestionMarkingInterval();
242 if (parameters.hasDefaultCongestionThreshold()) {
243 options.defaultCongestionThreshold = parameters.getDefaultCongestionThreshold();
246 if (parameters.hasMtu()) {
248 options.overrideMtu = std::min<uint64_t>(std::numeric_limits<ssize_t>::max(), parameters.getMtu());
251 linkService->setOptions(options);
255 FaceManager::updateFace(
const Interest& interest,
256 const ControlParameters& parameters,
257 const ndn::mgmt::CommandContinuation& done)
259 FaceId faceId = parameters.getFaceId();
261 auto incomingFaceIdTag = interest.getTag<lp::IncomingFaceIdTag>();
262 if (incomingFaceIdTag ==
nullptr) {
264 done(ControlResponse(404,
"No FaceId specified and IncomingFaceId not available"));
267 faceId = *incomingFaceIdTag;
270 Face* face = m_faceTable.
get(faceId);
271 if (face ==
nullptr) {
273 done(ControlResponse(404,
"Specified face does not exist"));
278 ControlParameters response;
279 bool areParamsValid =
true;
281 if (parameters.hasFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED) &&
282 parameters.getFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED) &&
283 face->
getScope() != ndn::nfd::FACE_SCOPE_LOCAL) {
284 NFD_LOG_TRACE(
"received request to enable local fields on non-local face");
285 areParamsValid =
false;
286 response.setFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED,
287 parameters.getFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED));
291 if (parameters.hasFacePersistency()) {
292 auto persistency = parameters.getFacePersistency();
294 NFD_LOG_TRACE(
"cannot change face persistency to " << persistency);
295 areParamsValid =
false;
296 response.setFacePersistency(persistency);
301 if (parameters.hasMtu()) {
302 auto mtu = parameters.getMtu();
304 auto actualMtu = std::min<uint64_t>(std::numeric_limits<ssize_t>::max(), mtu);
306 if (linkService ==
nullptr || !linkService->canOverrideMtuTo(actualMtu)) {
308 areParamsValid =
false;
309 response.setMtu(mtu);
313 if (!areParamsValid) {
314 done(ControlResponse(409,
"Invalid properties specified").setBody(response.wireEncode()));
319 if (parameters.hasFacePersistency()) {
326 done(ControlResponse(200,
"OK").setBody(response.wireEncode()));
330 FaceManager::destroyFace(
const ControlParameters& parameters,
331 const ndn::mgmt::CommandContinuation& done)
333 Face* face = m_faceTable.
get(parameters.getFaceId());
334 if (face !=
nullptr) {
338 done(ControlResponse(200,
"OK").setBody(parameters.wireEncode()));
345 to.setFaceId(face.
getId())
353 if (linkService !=
nullptr) {
354 const auto& options = linkService->
getOptions();
355 to.setFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED, options.allowLocalFields)
356 .setFlagBit(ndn::nfd::BIT_LP_RELIABILITY_ENABLED, options.reliabilityOptions.isEnabled)
357 .setFlagBit(ndn::nfd::BIT_CONGESTION_MARKING_ENABLED, options.allowCongestionMarking);
361 static ndn::nfd::FaceStatus
364 ndn::nfd::FaceStatus status;
368 if (expirationTime != time::steady_clock::TimePoint::max()) {
369 status.setExpirationPeriod(std::max(0_ms,
370 time::duration_cast<time::milliseconds>(expirationTime - now)));
374 if (linkService !=
nullptr) {
375 const auto& options = linkService->
getOptions();
376 status.setBaseCongestionMarkingInterval(options.baseCongestionMarkingInterval)
377 .setDefaultCongestionThreshold(options.defaultCongestionThreshold);
383 status.setNInInterests(counters.nInInterests)
384 .setNOutInterests(counters.nOutInterests)
385 .setNInData(counters.nInData)
386 .setNOutData(counters.nOutData)
387 .setNInNacks(counters.nInNacks)
388 .setNOutNacks(counters.nOutNacks)
389 .setNInBytes(counters.nInBytes)
390 .setNOutBytes(counters.nOutBytes);
396 FaceManager::listFaces(ndn::mgmt::StatusDatasetContext& context)
398 auto now = time::steady_clock::now();
399 for (
const auto& face : m_faceTable) {
401 context.append(status.wireEncode());
407 FaceManager::listChannels(ndn::mgmt::StatusDatasetContext& context)
410 for (
const auto* factory : factories) {
411 for (
const auto& channel : factory->getChannels()) {
412 ndn::nfd::ChannelStatus entry;
413 entry.setLocalUri(channel->getUri().toString());
414 context.append(entry.wireEncode());
423 if (filter.hasFaceId() &&
424 filter.getFaceId() !=
static_cast<uint64_t
>(face.
getId())) {
428 if (filter.hasUriScheme() &&
429 filter.getUriScheme() != face.
getRemoteUri().getScheme() &&
430 filter.getUriScheme() != face.
getLocalUri().getScheme()) {
434 if (filter.hasRemoteUri() &&
435 filter.getRemoteUri() != face.
getRemoteUri().toString()) {
439 if (filter.hasLocalUri() &&
440 filter.getLocalUri() != face.
getLocalUri().toString()) {
444 if (filter.hasFaceScope() &&
445 filter.getFaceScope() != face.
getScope()) {
449 if (filter.hasFacePersistency() &&
454 if (filter.hasLinkType() &&
463 FaceManager::queryFaces(
const Interest& interest,
464 ndn::mgmt::StatusDatasetContext& context)
466 ndn::nfd::FaceQueryFilter faceFilter;
468 faceFilter.wireDecode(interest.getName()[-1].blockFromValue());
470 catch (
const tlv::Error& e) {
472 return context.reject(ControlResponse(400,
"Malformed filter"));
475 auto now = time::steady_clock::now();
476 for (
const auto& face : m_faceTable) {
479 context.append(status.wireEncode());
486 FaceManager::notifyFaceEvent(
const Face& face, ndn::nfd::FaceEventKind kind)
488 ndn::nfd::FaceEventNotification notification;
489 notification.setKind(kind);
492 m_postNotification(notification.wireEncode());
496 FaceManager::connectFaceStateChangeSignal(
const Face& face)
503 if (newState == FaceState::UP) {
504 notifyFaceEvent(face, ndn::nfd::FACE_EVENT_UP);
506 else if (newState == FaceState::DOWN) {
507 notifyFaceEvent(face, ndn::nfd::FACE_EVENT_DOWN);
509 else if (newState == FaceState::CLOSED) {
511 m_faceStateChangeConn.erase(faceId);
FaceUri getRemoteUri() const
void registerStatusDatasetHandler(const std::string &verb, const ndn::mgmt::StatusDatasetHandler &handler)
const FaceCounters & getCounters() const
static void updateLinkServiceOptions(Face &face, const ControlParameters ¶meters)
const ssize_t MTU_UNLIMITED
indicates the transport has no limit on payload size
void setPersistency(ndn::nfd::FacePersistency persistency)
changes face persistency setting
ProtocolFactory * getFactoryByScheme(const std::string &scheme)
bool canChangePersistencyTo(ndn::nfd::FacePersistency newPersistency) const
check whether the face persistency can be changed to newPersistency
boost::logic::tribool wantCongestionMarking
optional< uint64_t > defaultCongestionThreshold
GenericLinkService is a LinkService that implements the NDNLPv2 protocol.
ndn::nfd::FacePersistency getPersistency() const
Face * get(FaceId id) const
get face by FaceId
signal::Signal< FaceTable, Face > afterAdd
Fires immediately after a face is added.
ndn::nfd::LinkType getLinkType() const
ndn::nfd::FaceScope getScope() const
FaceManager(FaceSystem &faceSystem, Dispatcher &dispatcher, CommandAuthenticator &authenticator)
std::set< const ProtocolFactory * > listProtocolFactories() const
Parameters used to set Transport properties or LinkService options on a newly created face...
void close()
Request that the face be closed.
Transport * getTransport() const
Provides ControlCommand authorization according to NFD configuration file.
A collection of common functions shared by all NFD managers, such as communicating with the dispatche...
static ndn::nfd::FaceStatus makeFaceStatus(const Face &face, const time::steady_clock::TimePoint &now)
void createFace(const CreateFaceRequest &req, const FaceCreatedCallback &onCreated, const FaceCreationFailedCallback &onFailure)
Create a unicast face.
optional< time::nanoseconds > baseCongestionMarkingInterval
static ControlParameters makeCreateFaceResponse(const Face &face)
signal::Signal< FaceTable, Face > beforeRemove
Fires immediately before a face is removed.
void add(shared_ptr< Face > face)
add a face
Copyright (c) 2014-2015, Regents of the University of California, Arizona Board of Regents...
TransportState FaceState
indicates the state of a face
static void copyMtu(const Face &face, T &to)
FaceUri getLocalUri() const
ndn::mgmt::PostNotification registerNotificationStream(const std::string &verb)
generalization of a network interface
static bool matchFilter(const ndn::nfd::FaceQueryFilter &filter, const Face &face)
LinkService * getLinkService() const
ndn::nfd::FacePersistency persistency
Provides support for an underlying protocol.
#define NFD_LOG_INIT(name)
const Options & getOptions() const
get Options used by GenericLinkService
entry point of the face system
signal::Signal< Transport, FaceState, FaceState > & afterStateChange
signals after face state changed
bool allowLocalFields
enables encoding of IncomingFaceId, and decoding of NextHopFaceId and CachePolicy ...
uint64_t FaceId
Identifies a face.
ssize_t getMtu() const
Returns face effective MTU.
const FaceId INVALID_FACEID
indicates an invalid FaceId
static ControlParameters makeUpdateFaceResponse(const Face &face)
static void copyFaceProperties(const Face &face, T &to)
time::steady_clock::TimePoint getExpirationTime() const