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);
90 Forwarder::onIncomingInterest(
const Interest& interest,
const FaceEndpoint& ingress)
93 NFD_LOG_DEBUG(
"onIncomingInterest in=" << ingress <<
" interest=" << interest.getName());
94 interest.setTag(make_shared<lp::IncomingFaceIdTag>(ingress.
face.
getId()));
98 if (interest.getHopLimit()) {
99 if (*interest.getHopLimit() == 0) {
100 NFD_LOG_DEBUG(
"onIncomingInterest in=" << ingress <<
" interest=" << interest.getName()
106 const_cast<Interest&
>(interest).setHopLimit(*interest.getHopLimit() - 1);
110 bool isViolatingLocalhost = ingress.
face.
getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL &&
112 if (isViolatingLocalhost) {
114 <<
" interest=" << interest.getName() <<
" violates /localhost");
120 bool hasDuplicateNonceInDnl = m_deadNonceList.
has(interest.getName(), interest.getNonce());
121 if (hasDuplicateNonceInDnl) {
123 this->onInterestLoop(interest, ingress);
128 if (!interest.getForwardingHint().empty() &&
131 <<
" interest=" << interest.getName() <<
" reaching-producer-region");
132 const_cast<Interest&
>(interest).setForwardingHint({});
136 shared_ptr<pit::Entry> pitEntry = m_pit.
insert(interest).first;
141 if (ingress.
face.
getLinkType() == ndn::nfd::LINK_TYPE_POINT_TO_POINT) {
145 if (hasDuplicateNonceInPit) {
147 this->onInterestLoop(interest, ingress);
152 if (!pitEntry->hasInRecords()) {
154 [=] (
const Interest& i,
const Data& d) { onContentStoreHit(i, ingress, pitEntry, d); },
155 [=] (
const Interest& i) { onContentStoreMiss(i, ingress, pitEntry); });
158 this->onContentStoreMiss(interest, ingress, pitEntry);
163 Forwarder::onInterestLoop(
const Interest& interest,
const FaceEndpoint& ingress)
166 if (ingress.face.getLinkType() != ndn::nfd::LINK_TYPE_POINT_TO_POINT) {
168 <<
" interest=" << interest.getName() <<
" drop");
172 NFD_LOG_DEBUG(
"onInterestLoop in=" << ingress <<
" interest=" << interest.getName()
173 <<
" send-Nack-duplicate");
177 lp::Nack nack(interest);
178 nack.setReason(lp::NackReason::DUPLICATE);
179 ingress.face.sendNack(nack);
183 Forwarder::onContentStoreMiss(
const Interest& interest,
const FaceEndpoint& ingress,
184 const shared_ptr<pit::Entry>& pitEntry)
186 NFD_LOG_DEBUG(
"onContentStoreMiss interest=" << interest.getName());
190 if (m_config.defaultHopLimit > 0 && !interest.getHopLimit()) {
191 const_cast<Interest&
>(interest).setHopLimit(m_config.defaultHopLimit);
195 pitEntry->insertOrUpdateInRecord(ingress.face, interest);
198 auto lastExpiring = std::max_element(pitEntry->in_begin(), pitEntry->in_end(),
199 [] (
const auto& a,
const auto& b) {
200 return a.getExpiry() < b.getExpiry();
202 auto lastExpiryFromNow = lastExpiring->getExpiry() - time::steady_clock::now();
203 this->setExpiryTimer(pitEntry, time::duration_cast<time::milliseconds>(lastExpiryFromNow));
206 auto nextHopTag = interest.getTag<lp::NextHopFaceIdTag>();
207 if (nextHopTag !=
nullptr) {
209 Face* nextHopFace = m_faceTable.
get(*nextHopTag);
210 if (nextHopFace !=
nullptr) {
211 NFD_LOG_DEBUG(
"onContentStoreMiss interest=" << interest.getName()
212 <<
" nexthop-faceid=" << nextHopFace->getId());
215 this->onOutgoingInterest(interest, *nextHopFace, pitEntry);
226 Forwarder::onContentStoreHit(
const Interest& interest,
const FaceEndpoint& ingress,
227 const shared_ptr<pit::Entry>& pitEntry,
const Data& data)
229 NFD_LOG_DEBUG(
"onContentStoreHit interest=" << interest.getName());
233 data.setTag(interest.getTag<lp::PitToken>());
236 pitEntry->isSatisfied =
true;
237 pitEntry->dataFreshnessPeriod = data.getFreshnessPeriod();
240 this->setExpiryTimer(pitEntry, 0_ms);
247 Forwarder::onOutgoingInterest(
const Interest& interest, Face& egress,
248 const shared_ptr<pit::Entry>& pitEntry)
251 if (interest.getHopLimit() == 0 && egress.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL) {
252 NFD_LOG_DEBUG(
"onOutgoingInterest out=" << egress.getId() <<
" interest=" << pitEntry->getName()
253 <<
" non-local hop-limit=0");
254 ++egress.getCounters().nOutHopLimitZero;
258 NFD_LOG_DEBUG(
"onOutgoingInterest out=" << egress.getId() <<
" interest=" << pitEntry->getName());
261 auto it = pitEntry->insertOrUpdateOutRecord(egress, interest);
262 BOOST_ASSERT(it != pitEntry->out_end());
265 egress.sendInterest(interest);
271 Forwarder::onInterestFinalize(
const shared_ptr<pit::Entry>& pitEntry)
273 NFD_LOG_DEBUG(
"onInterestFinalize interest=" << pitEntry->getName()
274 << (pitEntry->isSatisfied ?
" satisfied" :
" unsatisfied"));
277 this->insertDeadNonceList(*pitEntry,
nullptr);
280 if (pitEntry->isSatisfied) {
288 pitEntry->expiryTimer.cancel();
289 m_pit.
erase(pitEntry.get());
293 Forwarder::onIncomingData(
const Data& data,
const FaceEndpoint& ingress)
296 NFD_LOG_DEBUG(
"onIncomingData in=" << ingress <<
" data=" << data.getName());
297 data.setTag(make_shared<lp::IncomingFaceIdTag>(ingress.face.getId()));
301 bool isViolatingLocalhost = ingress.face.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL &&
303 if (isViolatingLocalhost) {
304 NFD_LOG_DEBUG(
"onIncomingData in=" << ingress <<
" data=" << data.getName() <<
" violates /localhost");
311 if (pitMatches.size() == 0) {
313 this->onDataUnsolicited(data, ingress);
321 if (pitMatches.size() == 1) {
322 auto& pitEntry = pitMatches.front();
324 NFD_LOG_DEBUG(
"onIncomingData matching=" << pitEntry->getName());
327 this->setExpiryTimer(pitEntry, 0_ms);
333 pitEntry->isSatisfied =
true;
334 pitEntry->dataFreshnessPeriod = data.getFreshnessPeriod();
337 this->insertDeadNonceList(*pitEntry, &ingress.face);
340 pitEntry->deleteOutRecord(ingress.face);
345 std::set<Face*> pendingDownstreams;
346 auto now = time::steady_clock::now();
348 for (
const auto& pitEntry : pitMatches) {
349 NFD_LOG_DEBUG(
"onIncomingData matching=" << pitEntry->getName());
352 for (
const pit::InRecord& inRecord : pitEntry->getInRecords()) {
353 if (inRecord.getExpiry() > now) {
354 pendingDownstreams.insert(&inRecord.getFace());
359 this->setExpiryTimer(pitEntry, 0_ms);
365 pitEntry->isSatisfied =
true;
366 pitEntry->dataFreshnessPeriod = data.getFreshnessPeriod();
369 this->insertDeadNonceList(*pitEntry, &ingress.face);
372 pitEntry->clearInRecords();
373 pitEntry->deleteOutRecord(ingress.face);
377 for (
const auto& pendingDownstream : pendingDownstreams) {
378 if (pendingDownstream->getId() == ingress.face.getId() &&
379 pendingDownstream->getLinkType() != ndn::nfd::LINK_TYPE_AD_HOC) {
383 this->onOutgoingData(data, *pendingDownstream);
389 Forwarder::onDataUnsolicited(
const Data& data,
const FaceEndpoint& ingress)
392 auto decision = m_unsolicitedDataPolicy->decide(ingress.face, data);
398 NFD_LOG_DEBUG(
"onDataUnsolicited in=" << ingress <<
" data=" << data.getName()
399 <<
" decision=" << decision);
404 Forwarder::onOutgoingData(
const Data& data, Face& egress)
407 NFD_LOG_WARN(
"onOutgoingData out=(invalid) data=" << data.getName());
410 NFD_LOG_DEBUG(
"onOutgoingData out=" << egress.getId() <<
" data=" << data.getName());
413 bool isViolatingLocalhost = egress.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL &&
415 if (isViolatingLocalhost) {
416 NFD_LOG_DEBUG(
"onOutgoingData out=" << egress.getId() <<
" data=" << data.getName()
417 <<
" violates /localhost");
425 egress.sendData(data);
432 Forwarder::onIncomingNack(
const lp::Nack& nack,
const FaceEndpoint& ingress)
435 nack.setTag(make_shared<lp::IncomingFaceIdTag>(ingress.face.getId()));
439 if (ingress.face.getLinkType() != ndn::nfd::LINK_TYPE_POINT_TO_POINT) {
441 <<
" nack=" << nack.getInterest().getName() <<
"~" << nack.getReason()
442 <<
" link-type=" << ingress.face.getLinkType());
447 shared_ptr<pit::Entry> pitEntry = m_pit.
find(nack.getInterest());
449 if (pitEntry ==
nullptr) {
450 NFD_LOG_DEBUG(
"onIncomingNack in=" << ingress <<
" nack=" << nack.getInterest().getName()
451 <<
"~" << nack.getReason() <<
" no-PIT-entry");
456 auto outRecord = pitEntry->getOutRecord(ingress.face);
458 if (outRecord == pitEntry->out_end()) {
459 NFD_LOG_DEBUG(
"onIncomingNack in=" << ingress <<
" nack=" << nack.getInterest().getName()
460 <<
"~" << nack.getReason() <<
" no-out-record");
465 if (nack.getInterest().getNonce() != outRecord->getLastNonce()) {
466 NFD_LOG_DEBUG(
"onIncomingNack in=" << ingress <<
" nack=" << nack.getInterest().getName()
467 <<
"~" << nack.getReason() <<
" wrong-Nonce " << nack.getInterest().getNonce()
468 <<
"!=" << outRecord->getLastNonce());
472 NFD_LOG_DEBUG(
"onIncomingNack in=" << ingress <<
" nack=" << nack.getInterest().getName()
473 <<
"~" << nack.getReason() <<
" OK");
476 outRecord->setIncomingNack(nack);
480 this->setExpiryTimer(pitEntry, 0_ms);
488 Forwarder::onOutgoingNack(
const lp::NackHeader& nack, Face& egress,
489 const shared_ptr<pit::Entry>& pitEntry)
493 <<
" nack=" << pitEntry->getInterest().getName() <<
"~" << nack.getReason());
498 auto inRecord = pitEntry->getInRecord(egress);
501 if (inRecord == pitEntry->in_end()) {
503 <<
" nack=" << pitEntry->getInterest().getName()
504 <<
"~" << nack.getReason() <<
" no-in-record");
509 if (egress.getLinkType() != ndn::nfd::LINK_TYPE_POINT_TO_POINT) {
511 <<
" nack=" << pitEntry->getInterest().getName() <<
"~" << nack.getReason()
512 <<
" link-type=" << egress.getLinkType());
517 <<
" nack=" << pitEntry->getInterest().getName()
518 <<
"~" << nack.getReason() <<
" OK");
521 lp::Nack nackPkt(inRecord->getInterest());
522 nackPkt.setHeader(nack);
525 pitEntry->deleteInRecord(egress);
528 egress.sendNack(nackPkt);
535 Forwarder::onDroppedInterest(
const Interest& interest, Face& egress)
541 Forwarder::onNewNextHop(
const Name& prefix,
const fib::NextHop& nextHop)
544 [&] (
const name_tree::Entry& nte) -> std::pair<bool, bool> {
550 if (nte.getFibEntry() !=
nullptr && nte.getName().size() > prefix.size()) {
551 return {false, false};
553 return {nte.hasPitEntries(),
true};
556 for (
const auto& nte : affectedEntries) {
557 for (
const auto& pitEntry : nte.getPitEntries()) {
558 m_strategyChoice.findEffectiveStrategy(*pitEntry).afterNewNextHop(nextHop, pitEntry);
564 Forwarder::setExpiryTimer(
const shared_ptr<pit::Entry>& pitEntry, time::milliseconds duration)
566 BOOST_ASSERT(pitEntry);
567 duration = std::max(duration, 0_ms);
569 pitEntry->expiryTimer.cancel();
570 pitEntry->expiryTimer =
getScheduler().schedule(duration, [=] { onInterestFinalize(pitEntry); });
574 Forwarder::insertDeadNonceList(pit::Entry& pitEntry,
const Face* upstream)
578 if (pitEntry.isSatisfied) {
579 BOOST_ASSERT(pitEntry.dataFreshnessPeriod >= 0_ms);
580 needDnl = pitEntry.getInterest().getMustBeFresh() &&
581 pitEntry.dataFreshnessPeriod < m_deadNonceList.getLifetime();
589 if (upstream ==
nullptr) {
591 const auto& outRecords = pitEntry.getOutRecords();
592 std::for_each(outRecords.begin(), outRecords.end(), [&] (
const auto& outRecord) {
593 m_deadNonceList.add(pitEntry.getName(), outRecord.getLastNonce());
598 auto outRecord = pitEntry.getOutRecord(*upstream);
599 if (outRecord != pitEntry.getOutRecords().end()) {
600 m_deadNonceList.add(pitEntry.getName(), outRecord->getLastNonce());
609 processConfig(std::forward<decltype(args)>(args)...);
614 Forwarder::processConfig(
const ConfigSection& configSection,
bool isDryRun,
const std::string&)
618 for (
const auto& pair : configSection) {
619 const std::string& key = pair.first;
620 if (key ==
"default_hop_limit") {
621 config.defaultHopLimit = ConfigFile::parseNumber<uint8_t>(pair,
CFG_FORWARDER);
624 NDN_THROW(ConfigFile::Error(
"Unrecognized option " +
CFG_FORWARDER +
"." + key));
This file contains common algorithms used by forwarding strategies.
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.
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.
PacketCounter nSatisfiedInterests
PacketCounter nUnsatisfiedInterests
PacketCounter nOutInterests
PacketCounter nInInterests
PacketCounter nUnsolicitedData
NameTree & getNameTree() noexcept
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
Returns whether the face is local or non-local for scope control purposes.
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.
FaceId getId() const noexcept
Returns the face ID.
signal::Signal< LinkService, Interest, EndpointId > & afterReceiveInterest
Signals on Interest received.
const FaceCounters & getCounters() const noexcept
ndn::nfd::LinkType getLinkType() const
Returns the link type of the face (point-to-point, multi-access, ...).
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.
#define NFD_LOG_INIT(name)
constexpr FaceId INVALID_FACEID
Indicates an invalid FaceId.
constexpr FaceId FACEID_CONTENT_STORE
Identifies a packet comes from the ContentStore.
std::variant< std::monostate, ethernet::Address, udp::Endpoint > EndpointId
Identifies a remote endpoint on the link.
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
The localhost scope ndn:/localhost.
const std::string CFG_FORWARDER
boost::property_tree::ptree ConfigSection
A configuration 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.