35 #include <ndn-cxx/lp/tags.hpp> 48 : m_faceTable(faceTable)
52 , m_measurements(m_nameTree)
53 , m_strategyChoice(*this)
57 [
this, &face] (
const Interest& interest,
const EndpointId& endpointId) {
61 [
this, &face] (
const Data& data,
const EndpointId& endpointId) {
65 [
this, &face] (
const lp::Nack& nack,
const EndpointId& endpointId) {
69 [
this, &face] (
const Interest& interest) {
70 this->onDroppedInterest(
FaceEndpoint(face, 0), interest);
88 Forwarder::onIncomingInterest(
const FaceEndpoint& ingress,
const Interest& interest)
91 NFD_LOG_DEBUG(
"onIncomingInterest in=" << ingress <<
" interest=" << interest.getName());
92 interest.setTag(make_shared<lp::IncomingFaceIdTag>(ingress.
face.
getId()));
96 bool isViolatingLocalhost = ingress.
face.
getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL &&
98 if (isViolatingLocalhost) {
100 <<
" interest=" << interest.getName() <<
" violates /localhost");
106 bool hasDuplicateNonceInDnl = m_deadNonceList.
has(interest.getName(), interest.getNonce());
107 if (hasDuplicateNonceInDnl) {
109 this->onInterestLoop(ingress, interest);
114 if (!interest.getForwardingHint().empty() &&
117 <<
" interest=" << interest.getName() <<
" reaching-producer-region");
118 const_cast<Interest&
>(interest).setForwardingHint({});
122 shared_ptr<pit::Entry> pitEntry = m_pit.
insert(interest).first;
127 if (ingress.
face.
getLinkType() == ndn::nfd::LINK_TYPE_POINT_TO_POINT) {
131 if (hasDuplicateNonceInPit) {
133 this->onInterestLoop(ingress, interest);
138 if (!pitEntry->hasInRecords()) {
140 bind(&Forwarder::onContentStoreHit,
this, ingress, pitEntry, _1, _2),
141 bind(&Forwarder::onContentStoreMiss,
this, ingress, pitEntry, _1));
144 this->onContentStoreMiss(ingress, pitEntry, interest);
149 Forwarder::onInterestLoop(
const FaceEndpoint& ingress,
const Interest& interest)
152 if (ingress.
face.
getLinkType() != ndn::nfd::LINK_TYPE_POINT_TO_POINT) {
154 <<
" interest=" << interest.getName() <<
" drop");
158 NFD_LOG_DEBUG(
"onInterestLoop in=" << ingress <<
" interest=" << interest.getName()
159 <<
" send-Nack-duplicate");
163 lp::Nack nack(interest);
164 nack.setReason(lp::NackReason::DUPLICATE);
169 Forwarder::onContentStoreMiss(
const FaceEndpoint& ingress,
170 const shared_ptr<pit::Entry>& pitEntry,
const Interest& interest)
172 NFD_LOG_DEBUG(
"onContentStoreMiss interest=" << interest.getName());
176 pitEntry->insertOrUpdateInRecord(ingress.
face, interest);
179 auto lastExpiring = std::max_element(pitEntry->in_begin(), pitEntry->in_end(),
180 [] (
const auto& a,
const auto& b) {
181 return a.getExpiry() < b.getExpiry();
183 auto lastExpiryFromNow = lastExpiring->getExpiry() - time::steady_clock::now();
184 this->setExpiryTimer(pitEntry, time::duration_cast<time::milliseconds>(lastExpiryFromNow));
187 auto nextHopTag = interest.getTag<lp::NextHopFaceIdTag>();
188 if (nextHopTag !=
nullptr) {
190 Face* nextHopFace = m_faceTable.
get(*nextHopTag);
191 if (nextHopFace !=
nullptr) {
192 NFD_LOG_DEBUG(
"onContentStoreMiss interest=" << interest.getName()
193 <<
" nexthop-faceid=" << nextHopFace->
getId());
196 this->onOutgoingInterest(pitEntry,
FaceEndpoint(*nextHopFace, 0), interest);
202 this->dispatchToStrategy(*pitEntry,
209 Forwarder::onContentStoreHit(
const FaceEndpoint& ingress,
const shared_ptr<pit::Entry>& pitEntry,
210 const Interest& interest,
const Data& data)
212 NFD_LOG_DEBUG(
"onContentStoreHit interest=" << interest.getName());
218 pitEntry->isSatisfied =
true;
219 pitEntry->dataFreshnessPeriod = data.getFreshnessPeriod();
222 this->setExpiryTimer(pitEntry, 0_ms);
225 this->dispatchToStrategy(*pitEntry,
230 Forwarder::onOutgoingInterest(
const shared_ptr<pit::Entry>& pitEntry,
233 NFD_LOG_DEBUG(
"onOutgoingInterest out=" << egress <<
" interest=" << pitEntry->getName());
236 pitEntry->insertOrUpdateOutRecord(egress.
face, interest);
244 Forwarder::onInterestFinalize(
const shared_ptr<pit::Entry>& pitEntry)
246 NFD_LOG_DEBUG(
"onInterestFinalize interest=" << pitEntry->getName()
247 << (pitEntry->isSatisfied ?
" satisfied" :
" unsatisfied"));
250 this->insertDeadNonceList(*pitEntry,
nullptr);
253 if (pitEntry->isSatisfied) {
261 pitEntry->expiryTimer.cancel();
262 m_pit.
erase(pitEntry.get());
266 Forwarder::onIncomingData(
const FaceEndpoint& ingress,
const Data& data)
269 NFD_LOG_DEBUG(
"onIncomingData in=" << ingress <<
" data=" << data.getName());
270 data.setTag(make_shared<lp::IncomingFaceIdTag>(ingress.
face.
getId()));
274 bool isViolatingLocalhost = ingress.
face.
getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL &&
276 if (isViolatingLocalhost) {
277 NFD_LOG_DEBUG(
"onIncomingData in=" << ingress <<
" data=" << data.getName() <<
" violates /localhost");
284 if (pitMatches.size() == 0) {
286 this->onDataUnsolicited(ingress, data);
294 if (pitMatches.size() == 1) {
295 auto& pitEntry = pitMatches.front();
297 NFD_LOG_DEBUG(
"onIncomingData matching=" << pitEntry->getName());
300 this->setExpiryTimer(pitEntry, 0_ms);
303 this->dispatchToStrategy(*pitEntry,
307 pitEntry->isSatisfied =
true;
308 pitEntry->dataFreshnessPeriod = data.getFreshnessPeriod();
311 this->insertDeadNonceList(*pitEntry, &ingress.
face);
314 pitEntry->deleteOutRecord(ingress.
face);
319 std::set<std::pair<Face*, EndpointId>> pendingDownstreams;
320 auto now = time::steady_clock::now();
322 for (
const auto& pitEntry : pitMatches) {
323 NFD_LOG_DEBUG(
"onIncomingData matching=" << pitEntry->getName());
326 for (
const pit::InRecord& inRecord : pitEntry->getInRecords()) {
327 if (inRecord.getExpiry() > now) {
328 pendingDownstreams.emplace(&inRecord.getFace(), 0);
333 this->setExpiryTimer(pitEntry, 0_ms);
336 this->dispatchToStrategy(*pitEntry,
340 pitEntry->isSatisfied =
true;
341 pitEntry->dataFreshnessPeriod = data.getFreshnessPeriod();
344 this->insertDeadNonceList(*pitEntry, &ingress.
face);
347 pitEntry->clearInRecords();
348 pitEntry->deleteOutRecord(ingress.
face);
352 for (
const auto& pendingDownstream : pendingDownstreams) {
353 if (pendingDownstream.first->getId() == ingress.
face.
getId() &&
354 pendingDownstream.second == ingress.
endpoint &&
355 pendingDownstream.first->getLinkType() != ndn::nfd::LINK_TYPE_AD_HOC) {
359 this->onOutgoingData(data,
FaceEndpoint(*pendingDownstream.first, pendingDownstream.second));
365 Forwarder::onDataUnsolicited(
const FaceEndpoint& ingress,
const Data& data)
374 NFD_LOG_DEBUG(
"onDataUnsolicited in=" << ingress <<
" data=" << data.getName() <<
" decision=" << decision);
378 Forwarder::onOutgoingData(
const Data& data,
const FaceEndpoint& egress)
381 NFD_LOG_WARN(
"onOutgoingData out=(invalid) data=" << data.getName());
384 NFD_LOG_DEBUG(
"onOutgoingData out=" << egress <<
" data=" << data.getName());
387 bool isViolatingLocalhost = egress.
face.
getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL &&
389 if (isViolatingLocalhost) {
390 NFD_LOG_DEBUG(
"onOutgoingData out=" << egress <<
" data=" << data.getName() <<
" violates /localhost");
403 Forwarder::onIncomingNack(
const FaceEndpoint& ingress,
const lp::Nack& nack)
406 nack.setTag(make_shared<lp::IncomingFaceIdTag>(ingress.
face.
getId()));
410 if (ingress.
face.
getLinkType() != ndn::nfd::LINK_TYPE_POINT_TO_POINT) {
412 <<
" nack=" << nack.getInterest().getName() <<
"~" << nack.getReason()
418 shared_ptr<pit::Entry> pitEntry = m_pit.
find(nack.getInterest());
420 if (pitEntry ==
nullptr) {
421 NFD_LOG_DEBUG(
"onIncomingNack in=" << ingress <<
" nack=" << nack.getInterest().getName()
422 <<
"~" << nack.getReason() <<
" no-PIT-entry");
427 auto outRecord = pitEntry->getOutRecord(ingress.
face);
429 if (outRecord == pitEntry->out_end()) {
430 NFD_LOG_DEBUG(
"onIncomingNack in=" << ingress <<
" nack=" << nack.getInterest().getName()
431 <<
"~" << nack.getReason() <<
" no-out-record");
436 if (nack.getInterest().getNonce() != outRecord->getLastNonce()) {
437 NFD_LOG_DEBUG(
"onIncomingNack in=" << ingress <<
" nack=" << nack.getInterest().getName()
438 <<
"~" << nack.getReason() <<
" wrong-Nonce " << nack.getInterest().getNonce()
439 <<
"!=" << outRecord->getLastNonce());
443 NFD_LOG_DEBUG(
"onIncomingNack in=" << ingress <<
" nack=" << nack.getInterest().getName()
444 <<
"~" << nack.getReason() <<
" OK");
447 outRecord->setIncomingNack(nack);
451 this->setExpiryTimer(pitEntry, 0_ms);
455 this->dispatchToStrategy(*pitEntry,
460 Forwarder::onOutgoingNack(
const shared_ptr<pit::Entry>& pitEntry,
465 <<
" nack=" << pitEntry->getInterest().getName() <<
"~" << nack.getReason());
470 auto inRecord = pitEntry->getInRecord(egress.
face);
473 if (inRecord == pitEntry->in_end()) {
475 <<
" nack=" << pitEntry->getInterest().getName()
476 <<
"~" << nack.getReason() <<
" no-in-record");
483 <<
" nack=" << pitEntry->getInterest().getName() <<
"~" << nack.getReason()
489 <<
" nack=" << pitEntry->getInterest().getName()
490 <<
"~" << nack.getReason() <<
" OK");
493 lp::Nack nackPkt(inRecord->getInterest());
494 nackPkt.setHeader(nack);
497 pitEntry->deleteInRecord(egress.
face);
505 Forwarder::onDroppedInterest(
const FaceEndpoint& egress,
const Interest& interest)
511 Forwarder::onNewNextHop(
const Name& prefix,
const fib::NextHop& nextHop)
522 if (nte.
getName().size() == 0 ||
530 else if (fibEntry ==
nullptr) {
531 return {
false,
true};
536 return {
false,
false};
540 for (
const auto& nte : affectedEntries) {
542 this->dispatchToStrategy(*pitEntry,
551 Forwarder::setExpiryTimer(
const shared_ptr<pit::Entry>& pitEntry, time::milliseconds duration)
553 BOOST_ASSERT(pitEntry);
554 BOOST_ASSERT(duration >= 0_ms);
556 pitEntry->expiryTimer.cancel();
557 pitEntry->expiryTimer =
getScheduler().schedule(duration, [=] { onInterestFinalize(pitEntry); });
561 Forwarder::insertDeadNonceList(
pit::Entry& pitEntry,
Face* upstream)
567 needDnl =
static_cast<bool>(pitEntry.
getInterest().getMustBeFresh()) &&
576 if (upstream ==
nullptr) {
579 std::for_each(outRecords.begin(), outRecords.end(), [&] (
const auto& outRecord) {
580 m_deadNonceList.
add(pitEntry.
getName(), outRecord.getLastNonce());
587 m_deadNonceList.
add(pitEntry.
getName(), outRecord->getLastNonce());
virtual void afterNewNextHop(const fib::NextHop &nextHop, const shared_ptr< pit::Entry > &pitEntry)
trigger after new nexthop is added
const EndpointId endpoint
void startProcessNewNextHop(const Name &prefix, const fib::NextHop &nextHop)
start new nexthop processing
bool isSatisfied
Indicates whether this PIT entry is satisfied.
OutRecordCollection::iterator getOutRecord(const Face &face)
get the out-record for face
void cleanupOnFaceRemoval(NameTree &nt, Fib &fib, Pit &pit, const Face &face)
cleanup tables when a face is destroyed
PacketCounter nInInterests
PacketCounter nUnsatisfiedInterests
signal::Signal< LinkService, Interest, EndpointId > & afterReceiveInterest
signals on Interest received
void sendData(const Data &data, const EndpointId &endpointId)
send Data to endpointId
virtual void afterReceiveInterest(const FaceEndpoint &ingress, const Interest &interest, const shared_ptr< pit::Entry > &pitEntry)=0
trigger after Interest is received
signal::Signal< Fib, Name, NextHop > afterNewNextHop
signals on Fib entry nexthop creation
static const Name & getStrategyName()
signal::Signal< LinkService, Data, EndpointId > & afterReceiveData
signals on Data received
void startProcessData(const FaceEndpoint &ingress, const Data &data)
start incoming Data processing
void add(const Name &name, uint32_t nonce)
Records name+nonce.
Face * get(FaceId id) const
get face by FaceId
void sendInterest(const Interest &interest, const EndpointId &endpointId)
send Interest to endpointId
bool isInProducerRegion(const DelegationList &forwardingHint) const
determines whether an Interest has reached a producer region
shared_ptr< Entry > find(const Interest &interest) const
Finds a PIT entry for interest.
uint64_t EndpointId
Identifies a remote endpoint on the link.
signal::Signal< FaceTable, Face > afterAdd
Fires immediately after a face is added.
ndn::nfd::LinkType getLinkType() const
ndn::nfd::FaceScope getScope() const
PacketCounter nOutInterests
fib::Entry * getFibEntry() const
const std::vector< shared_ptr< pit::Entry > > & getPitEntries() const
DropAllUnsolicitedDataPolicy DefaultUnsolicitedDataPolicy
the default UnsolicitedDataPolicy
virtual void afterReceiveNack(const FaceEndpoint &ingress, const lp::Nack &nack, const shared_ptr< pit::Entry > &pitEntry)
trigger after Nack is received
Scheduler & getScheduler()
Returns the global Scheduler instance for the calling thread.
fw::Strategy & findEffectiveStrategy(const Name &prefix) const
Get effective strategy for prefix.
Forwarder(FaceTable &faceTable)
DataMatchResult findAllDataMatches(const Data &data) const
Performs a Data match.
const Interest & getInterest() const
signal::Signal< FaceTable, Face > beforeRemove
Fires immediately before a face is removed.
Represents a face-endpoint pair in the forwarder.
Copyright (c) 2014-2015, Regents of the University of California, Arizona Board of Regents...
void insert(const Data &data, bool isUnsolicited=false)
inserts a Data packet
Contains information about an Interest from an incoming face.
const Name & getName() const
time::nanoseconds getLifetime() const
void setDefaultStrategy(const Name &strategyName)
Set the default strategy.
Range partialEnumerate(const Name &prefix, const EntrySubTreeSelector &entrySubTreeSelector=AnyEntrySubTree()) const
Enumerate all entries under a prefix.
strategy_choice::Entry * getStrategyChoiceEntry() const
std::pair< shared_ptr< Entry >, bool > insert(const Interest &interest)
Inserts a PIT entry for interest.
const Name & getName() const
generalization of a network interface
bool hasPendingOutRecords(const pit::Entry &pitEntry)
determine whether pitEntry has any pending out-records
const Name LOCALHOST
ndn:/localhost
An entry in the name tree.
no duplicate Nonce is found
virtual void afterContentStoreHit(const shared_ptr< pit::Entry > &pitEntry, const FaceEndpoint &ingress, const Data &data)
trigger after a Data is matched in CS
const OutRecordCollection & getOutRecords() const
int findDuplicateNonce(const pit::Entry &pitEntry, uint32_t nonce, const Face &face)
determine whether pitEntry has duplicate Nonce nonce
static Name getDefaultStrategyName()
represents a forwarding strategy
This file contains common algorithms used by forwarding strategies.
fw::Strategy & getStrategy() const
#define NFD_LOG_INIT(name)
UnsolicitedDataDecision
a decision made by UnsolicitedDataPolicy
virtual void afterReceiveData(const shared_ptr< pit::Entry > &pitEntry, const FaceEndpoint &ingress, const Data &data)
trigger after Data is received
void startProcessInterest(const FaceEndpoint &ingress, const Interest &interest)
start incoming Interest processing
the Data should be cached in the ContentStore
An unordered iterable of all PIT entries matching Data.
void erase(Entry *entry)
Deletes an entry.
signal::Signal< LinkService, lp::Nack, EndpointId > & afterReceiveNack
signals on Nack received
signal::Signal< LinkService, Interest > & onDroppedInterest
signals on Interest dropped by reliability system for exceeding allowed number of retx ...
time::milliseconds dataFreshnessPeriod
Data freshness period.
void startProcessNack(const FaceEndpoint &ingress, const lp::Nack &nack)
start incoming Nack processing
const FaceId FACEID_CONTENT_STORE
identifies a packet comes from the ContentStore
Represents a nexthop record in a FIB entry.
PacketCounter nSatisfiedInterests
const FaceId INVALID_FACEID
indicates an invalid FaceId
void find(const Interest &interest, HitCallback &&hit, MissCallback &&miss) const
finds the best matching Data packet
virtual void beforeSatisfyInterest(const shared_ptr< pit::Entry > &pitEntry, const FaceEndpoint &ingress, const Data &data)
trigger before PIT entry is satisfied
bool hasPitEntries() const
bool has(const Name &name, uint32_t nonce) const
Determines if name+nonce exists.
bool wantNewNextHopTrigger() const
void sendNack(const lp::Nack &nack, const EndpointId &endpointId)
send Nack to endpointId