36 #include <ndn-cxx/lp/pit-token.hpp>
37 #include <ndn-cxx/lp/tags.hpp>
52 : m_faceTable(faceTable)
56 , m_measurements(m_nameTree)
57 , m_strategyChoice(*this)
61 [
this, &face] (
const Interest& interest,
const EndpointId& endpointId) {
62 this->onIncomingInterest(interest,
FaceEndpoint(
const_cast<Face&
>(face), endpointId));
65 [
this, &face] (
const Data& data,
const EndpointId& endpointId) {
66 this->onIncomingData(data,
FaceEndpoint(
const_cast<Face&
>(face), endpointId));
69 [
this, &face] (
const lp::Nack& nack,
const EndpointId& endpointId) {
70 this->onIncomingNack(nack,
FaceEndpoint(
const_cast<Face&
>(face), endpointId));
73 [
this, &face] (
const Interest& interest) {
74 this->onDroppedInterest(interest,
const_cast<Face&
>(face));
83 this->onNewNextHop(prefix, nextHop);
92 Forwarder::onIncomingInterest(
const Interest& interest,
const FaceEndpoint& ingress)
95 NFD_LOG_DEBUG(
"onIncomingInterest in=" << ingress <<
" interest=" << interest.getName());
96 interest.setTag(make_shared<lp::IncomingFaceIdTag>(ingress.
face.
getId()));
100 if (interest.getHopLimit()) {
101 if (*interest.getHopLimit() == 0) {
102 NFD_LOG_DEBUG(
"onIncomingInterest in=" << ingress <<
" interest=" << interest.getName()
108 const_cast<Interest&
>(interest).setHopLimit(*interest.getHopLimit() - 1);
112 bool isViolatingLocalhost = ingress.
face.
getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL &&
114 if (isViolatingLocalhost) {
116 <<
" interest=" << interest.getName() <<
" violates /localhost");
122 bool hasDuplicateNonceInDnl = m_deadNonceList.
has(interest.getName(), interest.getNonce());
123 if (hasDuplicateNonceInDnl) {
125 this->onInterestLoop(interest, ingress);
130 if (!interest.getForwardingHint().empty() &&
133 <<
" interest=" << interest.getName() <<
" reaching-producer-region");
134 const_cast<Interest&
>(interest).setForwardingHint({});
138 shared_ptr<pit::Entry> pitEntry = m_pit.
insert(interest).first;
143 if (ingress.
face.
getLinkType() == ndn::nfd::LINK_TYPE_POINT_TO_POINT) {
147 if (hasDuplicateNonceInPit) {
149 this->onInterestLoop(interest, ingress);
154 if (!pitEntry->hasInRecords()) {
156 [=] (
const Interest& i,
const Data& d) { onContentStoreHit(i, ingress, pitEntry, d); },
157 [=] (
const Interest& i) { onContentStoreMiss(i, ingress, pitEntry); });
160 this->onContentStoreMiss(interest, ingress, pitEntry);
165 Forwarder::onInterestLoop(
const Interest& interest,
const FaceEndpoint& ingress)
168 if (ingress.face.getLinkType() != ndn::nfd::LINK_TYPE_POINT_TO_POINT) {
170 <<
" interest=" << interest.getName() <<
" drop");
174 NFD_LOG_DEBUG(
"onInterestLoop in=" << ingress <<
" interest=" << interest.getName()
175 <<
" send-Nack-duplicate");
179 lp::Nack nack(interest);
180 nack.setReason(lp::NackReason::DUPLICATE);
181 ingress.face.sendNack(nack);
185 Forwarder::onContentStoreMiss(
const Interest& interest,
const FaceEndpoint& ingress,
186 const shared_ptr<pit::Entry>& pitEntry)
188 NFD_LOG_DEBUG(
"onContentStoreMiss interest=" << interest.getName());
192 if (m_config.defaultHopLimit > 0 && !interest.getHopLimit()) {
193 const_cast<Interest&
>(interest).setHopLimit(m_config.defaultHopLimit);
197 pitEntry->insertOrUpdateInRecord(ingress.face, interest);
200 auto lastExpiring = std::max_element(pitEntry->in_begin(), pitEntry->in_end(),
201 [] (
const auto& a,
const auto& b) {
202 return a.getExpiry() < b.getExpiry();
204 auto lastExpiryFromNow = lastExpiring->getExpiry() - time::steady_clock::now();
205 this->setExpiryTimer(pitEntry, time::duration_cast<time::milliseconds>(lastExpiryFromNow));
208 auto nextHopTag = interest.getTag<lp::NextHopFaceIdTag>();
209 if (nextHopTag !=
nullptr) {
211 Face* nextHopFace = m_faceTable.
get(*nextHopTag);
212 if (nextHopFace !=
nullptr) {
213 NFD_LOG_DEBUG(
"onContentStoreMiss interest=" << interest.getName()
214 <<
" nexthop-faceid=" << nextHopFace->getId());
217 this->onOutgoingInterest(interest, *nextHopFace, pitEntry);
228 Forwarder::onContentStoreHit(
const Interest& interest,
const FaceEndpoint& ingress,
229 const shared_ptr<pit::Entry>& pitEntry,
const Data& data)
231 NFD_LOG_DEBUG(
"onContentStoreHit interest=" << interest.getName());
235 data.setTag(interest.getTag<lp::PitToken>());
238 pitEntry->isSatisfied =
true;
239 pitEntry->dataFreshnessPeriod = data.getFreshnessPeriod();
242 this->setExpiryTimer(pitEntry, 0_ms);
249 Forwarder::onOutgoingInterest(
const Interest& interest, Face& egress,
250 const shared_ptr<pit::Entry>& pitEntry)
253 if (interest.getHopLimit() == 0 && egress.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL) {
254 NFD_LOG_DEBUG(
"onOutgoingInterest out=" << egress.getId() <<
" interest=" << pitEntry->getName()
255 <<
" non-local hop-limit=0");
256 ++egress.getCounters().nOutHopLimitZero;
260 NFD_LOG_DEBUG(
"onOutgoingInterest out=" << egress.getId() <<
" interest=" << pitEntry->getName());
263 auto it = pitEntry->insertOrUpdateOutRecord(egress, interest);
264 BOOST_ASSERT(it != pitEntry->out_end());
267 egress.sendInterest(interest);
273 Forwarder::onInterestFinalize(
const shared_ptr<pit::Entry>& pitEntry)
275 NFD_LOG_DEBUG(
"onInterestFinalize interest=" << pitEntry->getName()
276 << (pitEntry->isSatisfied ?
" satisfied" :
" unsatisfied"));
279 this->insertDeadNonceList(*pitEntry,
nullptr);
282 if (pitEntry->isSatisfied) {
290 pitEntry->expiryTimer.cancel();
291 m_pit.
erase(pitEntry.get());
295 Forwarder::onIncomingData(
const Data& data,
const FaceEndpoint& ingress)
298 NFD_LOG_DEBUG(
"onIncomingData in=" << ingress <<
" data=" << data.getName());
299 data.setTag(make_shared<lp::IncomingFaceIdTag>(ingress.face.getId()));
303 bool isViolatingLocalhost = ingress.face.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL &&
305 if (isViolatingLocalhost) {
306 NFD_LOG_DEBUG(
"onIncomingData in=" << ingress <<
" data=" << data.getName() <<
" violates /localhost");
313 if (pitMatches.size() == 0) {
315 this->onDataUnsolicited(data, ingress);
323 if (pitMatches.size() == 1) {
324 auto& pitEntry = pitMatches.front();
326 NFD_LOG_DEBUG(
"onIncomingData matching=" << pitEntry->getName());
329 this->setExpiryTimer(pitEntry, 0_ms);
335 pitEntry->isSatisfied =
true;
336 pitEntry->dataFreshnessPeriod = data.getFreshnessPeriod();
339 this->insertDeadNonceList(*pitEntry, &ingress.face);
342 pitEntry->deleteOutRecord(ingress.face);
347 std::set<Face*> pendingDownstreams;
348 auto now = time::steady_clock::now();
350 for (
const auto& pitEntry : pitMatches) {
351 NFD_LOG_DEBUG(
"onIncomingData matching=" << pitEntry->getName());
354 for (
const pit::InRecord& inRecord : pitEntry->getInRecords()) {
355 if (inRecord.getExpiry() > now) {
356 pendingDownstreams.insert(&inRecord.getFace());
361 this->setExpiryTimer(pitEntry, 0_ms);
367 pitEntry->isSatisfied =
true;
368 pitEntry->dataFreshnessPeriod = data.getFreshnessPeriod();
371 this->insertDeadNonceList(*pitEntry, &ingress.face);
374 pitEntry->clearInRecords();
375 pitEntry->deleteOutRecord(ingress.face);
379 for (
const auto& pendingDownstream : pendingDownstreams) {
380 if (pendingDownstream->getId() == ingress.face.getId() &&
381 pendingDownstream->getLinkType() != ndn::nfd::LINK_TYPE_AD_HOC) {
385 this->onOutgoingData(data, *pendingDownstream);
391 Forwarder::onDataUnsolicited(
const Data& data,
const FaceEndpoint& ingress)
394 auto decision = m_unsolicitedDataPolicy->decide(ingress.face, data);
400 NFD_LOG_DEBUG(
"onDataUnsolicited in=" << ingress <<
" data=" << data.getName()
401 <<
" decision=" << decision);
406 Forwarder::onOutgoingData(
const Data& data, Face& egress)
409 NFD_LOG_WARN(
"onOutgoingData out=(invalid) data=" << data.getName());
412 NFD_LOG_DEBUG(
"onOutgoingData out=" << egress.getId() <<
" data=" << data.getName());
415 bool isViolatingLocalhost = egress.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL &&
417 if (isViolatingLocalhost) {
418 NFD_LOG_DEBUG(
"onOutgoingData out=" << egress.getId() <<
" data=" << data.getName()
419 <<
" violates /localhost");
427 egress.sendData(data);
434 Forwarder::onIncomingNack(
const lp::Nack& nack,
const FaceEndpoint& ingress)
437 nack.setTag(make_shared<lp::IncomingFaceIdTag>(ingress.face.getId()));
441 if (ingress.face.getLinkType() != ndn::nfd::LINK_TYPE_POINT_TO_POINT) {
443 <<
" nack=" << nack.getInterest().getName() <<
"~" << nack.getReason()
444 <<
" link-type=" << ingress.face.getLinkType());
449 shared_ptr<pit::Entry> pitEntry = m_pit.
find(nack.getInterest());
451 if (pitEntry ==
nullptr) {
452 NFD_LOG_DEBUG(
"onIncomingNack in=" << ingress <<
" nack=" << nack.getInterest().getName()
453 <<
"~" << nack.getReason() <<
" no-PIT-entry");
458 auto outRecord = pitEntry->getOutRecord(ingress.face);
460 if (outRecord == pitEntry->out_end()) {
461 NFD_LOG_DEBUG(
"onIncomingNack in=" << ingress <<
" nack=" << nack.getInterest().getName()
462 <<
"~" << nack.getReason() <<
" no-out-record");
467 if (nack.getInterest().getNonce() != outRecord->getLastNonce()) {
468 NFD_LOG_DEBUG(
"onIncomingNack in=" << ingress <<
" nack=" << nack.getInterest().getName()
469 <<
"~" << nack.getReason() <<
" wrong-Nonce " << nack.getInterest().getNonce()
470 <<
"!=" << outRecord->getLastNonce());
474 NFD_LOG_DEBUG(
"onIncomingNack in=" << ingress <<
" nack=" << nack.getInterest().getName()
475 <<
"~" << nack.getReason() <<
" OK");
478 outRecord->setIncomingNack(nack);
482 this->setExpiryTimer(pitEntry, 0_ms);
490 Forwarder::onOutgoingNack(
const lp::NackHeader& nack, Face& egress,
491 const shared_ptr<pit::Entry>& pitEntry)
495 <<
" nack=" << pitEntry->getInterest().getName() <<
"~" << nack.getReason());
500 auto inRecord = pitEntry->getInRecord(egress);
503 if (inRecord == pitEntry->in_end()) {
505 <<
" nack=" << pitEntry->getInterest().getName()
506 <<
"~" << nack.getReason() <<
" no-in-record");
511 if (egress.getLinkType() != ndn::nfd::LINK_TYPE_POINT_TO_POINT) {
513 <<
" nack=" << pitEntry->getInterest().getName() <<
"~" << nack.getReason()
514 <<
" link-type=" << egress.getLinkType());
519 <<
" nack=" << pitEntry->getInterest().getName()
520 <<
"~" << nack.getReason() <<
" OK");
523 lp::Nack nackPkt(inRecord->getInterest());
524 nackPkt.setHeader(nack);
527 pitEntry->deleteInRecord(egress);
530 egress.sendNack(nackPkt);
537 Forwarder::onDroppedInterest(
const Interest& interest, Face& egress)
543 Forwarder::onNewNextHop(
const Name& prefix,
const fib::NextHop& nextHop)
546 [&] (
const name_tree::Entry& nte) -> std::pair<bool, bool> {
552 if (nte.getFibEntry() !=
nullptr && nte.getName().size() > prefix.size()) {
553 return {false, false};
555 return {nte.hasPitEntries(),
true};
558 for (
const auto& nte : affectedEntries) {
559 for (
const auto& pitEntry : nte.getPitEntries()) {
560 m_strategyChoice.findEffectiveStrategy(*pitEntry).afterNewNextHop(nextHop, pitEntry);
566 Forwarder::setExpiryTimer(
const shared_ptr<pit::Entry>& pitEntry, time::milliseconds duration)
568 BOOST_ASSERT(pitEntry);
569 duration = std::max(duration, 0_ms);
571 pitEntry->expiryTimer.cancel();
572 pitEntry->expiryTimer =
getScheduler().schedule(duration, [=] { onInterestFinalize(pitEntry); });
576 Forwarder::insertDeadNonceList(pit::Entry& pitEntry,
const Face* upstream)
580 if (pitEntry.isSatisfied) {
581 BOOST_ASSERT(pitEntry.dataFreshnessPeriod >= 0_ms);
582 needDnl = pitEntry.getInterest().getMustBeFresh() &&
583 pitEntry.dataFreshnessPeriod < m_deadNonceList.getLifetime();
591 if (upstream ==
nullptr) {
593 const auto& outRecords = pitEntry.getOutRecords();
594 std::for_each(outRecords.begin(), outRecords.end(), [&] (
const auto& outRecord) {
595 m_deadNonceList.add(pitEntry.getName(), outRecord.getLastNonce());
600 auto outRecord = pitEntry.getOutRecord(*upstream);
601 if (outRecord != pitEntry.getOutRecords().end()) {
602 m_deadNonceList.add(pitEntry.getName(), outRecord->getLastNonce());
611 processConfig(std::forward<decltype(args)>(args)...);
616 Forwarder::processConfig(
const ConfigSection& configSection,
bool isDryRun,
const std::string&)
620 for (
const auto& pair : configSection) {
621 const std::string& key = pair.first;
622 if (key ==
"default_hop_limit") {
623 config.defaultHopLimit = ConfigFile::parseNumber<uint8_t>(pair,
CFG_FORWARDER);
626 NDN_THROW(ConfigFile::Error(
"Unrecognized option " +
CFG_FORWARDER +
"." + key));
configuration file parsing utility
void addSectionHandler(const std::string §ionName, ConfigSectionHandler subscriber)
setup notification of configuration file sections
bool has(const Name &name, Interest::Nonce nonce) const
Determines if name+nonce is in the list.
Represents a face-endpoint pair in the forwarder.
Face * get(FaceId id) const
get face by FaceId
signal::Signal< FaceTable, Face > beforeRemove
Fires immediately before a face is removed.
signal::Signal< FaceTable, Face > afterAdd
Fires immediately after a face is added.
PacketCounter nSatisfiedInterests
PacketCounter nUnsatisfiedInterests
PacketCounter nOutInterests
PacketCounter nInInterests
PacketCounter nUnsolicitedData
Forwarder(FaceTable &faceTable)
bool isInProducerRegion(span< const Name > forwardingHint) const
determines whether an Interest has reached a producer region
void insert(const Data &data, bool isUnsolicited=false)
inserts a Data packet
void find(const Interest &interest, HitCallback &&hit, MissCallback &&miss) const
finds the best matching Data packet
PacketCounter nInHopLimitZero
count of incoming Interests dropped due to HopLimit == 0
generalization of a network interface
ndn::nfd::FaceScope getScope() const
signal::Signal< LinkService, Data, EndpointId > & afterReceiveData
signals on Data received
signal::Signal< LinkService, Interest > & onDroppedInterest
signals on Interest dropped by reliability system for exceeding allowed number of retx
signal::Signal< LinkService, lp::Nack, EndpointId > & afterReceiveNack
signals on Nack received
signal::Signal< LinkService, Interest, EndpointId > & afterReceiveInterest
signals on Interest received
const FaceCounters & getCounters() const
ndn::nfd::LinkType getLinkType() const
signal::Signal< Fib, Name, NextHop > afterNewNextHop
signals on Fib entry nexthop creation
Represents a nexthop record in a FIB entry.
static const Name & getStrategyName()
virtual void onDroppedInterest(const Interest &interest, Face &egress)
Trigger after an Interest is dropped (e.g., for exceeding allowed retransmissions).
virtual void afterContentStoreHit(const Data &data, const FaceEndpoint &ingress, const shared_ptr< pit::Entry > &pitEntry)
Trigger after a matching Data is found in the Content Store.
virtual void afterReceiveNack(const lp::Nack &nack, const FaceEndpoint &ingress, const shared_ptr< pit::Entry > &pitEntry)
Trigger after a Nack is received.
virtual void afterReceiveInterest(const Interest &interest, const FaceEndpoint &ingress, const shared_ptr< pit::Entry > &pitEntry)=0
Trigger after an Interest is received.
virtual void afterReceiveData(const Data &data, const FaceEndpoint &ingress, const shared_ptr< pit::Entry > &pitEntry)
Trigger after Data is received.
virtual void beforeSatisfyInterest(const Data &data, const FaceEndpoint &ingress, const shared_ptr< pit::Entry > &pitEntry)
Trigger before a PIT entry is satisfied.
Range partialEnumerate(const Name &prefix, const EntrySubTreeSelector &entrySubTreeSelector=AnyEntrySubTree()) const
Enumerate all entries under a prefix.
std::pair< shared_ptr< Entry >, bool > insert(const Interest &interest)
Inserts a PIT entry for interest.
shared_ptr< Entry > find(const Interest &interest) const
Finds a PIT entry for interest.
DataMatchResult findAllDataMatches(const Data &data) const
Performs a Data match.
void erase(Entry *entry)
Deletes an entry.
void setDefaultStrategy(const Name &strategyName)
Set the default strategy.
fw::Strategy & findEffectiveStrategy(const Name &prefix) const
Get effective strategy for prefix.
This file contains common algorithms used by forwarding strategies.
#define NFD_LOG_INIT(name)
uint64_t EndpointId
Identifies a remote endpoint on the link.
const FaceId INVALID_FACEID
indicates an invalid FaceId
const FaceId FACEID_CONTENT_STORE
identifies a packet comes from the ContentStore
DropAllUnsolicitedDataPolicy DefaultUnsolicitedDataPolicy
The default UnsolicitedDataPolicy.
@ CACHE
the Data should be cached in the ContentStore
@ DUPLICATE_NONCE_NONE
no duplicate Nonce is found
@ DUPLICATE_NONCE_IN_SAME
in-record of same face
int findDuplicateNonce(const pit::Entry &pitEntry, Interest::Nonce nonce, const Face &face)
determine whether pitEntry has duplicate Nonce nonce
bool hasPendingOutRecords(const pit::Entry &pitEntry)
determine whether pitEntry has any pending out-records
void setConfigFile(ConfigFile &config)
std::vector< shared_ptr< Entry > > DataMatchResult
const Name LOCALHOST
ndn:/localhost
Copyright (c) 2014-2015, Regents of the University of California, Arizona Board of Regents,...
const std::string CFG_FORWARDER
boost::property_tree::ptree ConfigSection
a config file section
void cleanupOnFaceRemoval(NameTree &nt, Fib &fib, Pit &pit, const Face &face)
cleanup tables when a face is destroyed
static Name getDefaultStrategyName()
Scheduler & getScheduler()
Returns the global Scheduler instance for the calling thread.