30 #include <ndn-cxx/lp/pit-token.hpp>
32 #include <boost/range/adaptor/map.hpp>
33 #include <boost/range/algorithm/copy.hpp>
34 #include <unordered_set>
41 Strategy::getRegistry()
43 static Registry registry;
47 Strategy::Registry::const_iterator
48 Strategy::find(
const Name& instanceName)
50 const Registry& registry = getRegistry();
56 auto found = registry.lower_bound(parsed.strategyName);
57 if (found != registry.end()) {
58 if (parsed.strategyName.getPrefix(-1).isPrefixOf(found->first)) {
59 NFD_LOG_TRACE(
"find " << instanceName <<
" versioned found=" << found->first);
64 NFD_LOG_TRACE(
"find " << instanceName <<
" versioned not-found");
65 return registry.end();
70 if (!parsed.strategyName.empty()) {
71 auto found = registry.lower_bound(parsed.strategyName.getSuccessor());
72 if (found != registry.begin()) {
74 if (parsed.strategyName.isPrefixOf(found->first)) {
75 NFD_LOG_TRACE(
"find " << instanceName <<
" unversioned found=" << found->first);
81 NFD_LOG_TRACE(
"find " << instanceName <<
" unversioned not-found");
82 return registry.end();
88 return Strategy::find(instanceName) != getRegistry().end();
94 auto found = Strategy::find(instanceName);
95 if (found == getRegistry().end()) {
100 unique_ptr<Strategy> instance = found->second(forwarder, instanceName);
101 NFD_LOG_DEBUG(
"create " << instanceName <<
" found=" << found->first
102 <<
" created=" << instance->getInstanceName());
103 BOOST_ASSERT(!instance->getInstanceName().empty());
110 return Strategy::find(instanceNameA) == Strategy::find(instanceNameB);
116 std::set<Name> strategyNames;
117 boost::copy(getRegistry() | boost::adaptors::map_keys,
118 std::inserter(strategyNames, strategyNames.end()));
119 return strategyNames;
125 for (ssize_t i = input.size() - 1; i > 0; --i) {
126 if (input[i].isVersion()) {
127 return {input.getPrefix(i + 1), input[i].toVersion(), input.getSubName(i + 1)};
130 return {input, std::nullopt, PartialName()};
136 BOOST_ASSERT(strategyName.at(-1).isVersion());
138 bool hasVersion = std::any_of(input.rbegin(), input.rend(),
139 [] (
const auto& comp) { return comp.isVersion(); });
140 return hasVersion ? input : Name(input).append(strategyName.at(-1));
148 for (
const auto& component : params) {
149 auto sep = std::find(component.value_begin(), component.value_end(),
'~');
150 if (sep == component.value_end()) {
151 NDN_THROW(std::invalid_argument(
"Strategy parameters format is (<parameter>~<value>)*"));
154 std::string p(component.value_begin(), sep);
155 std::advance(sep, 1);
156 std::string v(sep, component.value_end());
157 if (p.empty() || v.empty()) {
158 NDN_THROW(std::invalid_argument(
"Strategy parameter name and value cannot be empty"));
160 parsed[std::move(p)] = std::move(v);
167 : afterAddFace(forwarder.m_faceTable.afterAdd)
168 , beforeRemoveFace(forwarder.m_faceTable.beforeRemove)
169 , m_forwarder(forwarder)
170 , m_measurements(m_forwarder.getMeasurements(), m_forwarder.getStrategyChoice(), *this)
179 NFD_LOG_DEBUG(
"onInterestLoop in=" << ingress <<
" name=" << interest.getName());
181 lp::Nack nack(interest);
182 nack.setReason(lp::NackReason::DUPLICATE);
188 const shared_ptr<pit::Entry>& pitEntry)
190 NFD_LOG_DEBUG(
"afterContentStoreHit pitEntry=" << pitEntry->getName()
191 <<
" in=" << ingress <<
" data=" << data.getName());
198 const shared_ptr<pit::Entry>& pitEntry)
200 NFD_LOG_DEBUG(
"beforeSatisfyInterest pitEntry=" << pitEntry->getName()
201 <<
" in=" << ingress <<
" data=" << data.getName());
206 const shared_ptr<pit::Entry>& pitEntry)
208 NFD_LOG_DEBUG(
"afterReceiveData pitEntry=" << pitEntry->getName()
209 <<
" in=" << ingress <<
" data=" << data.getName());
217 const shared_ptr<pit::Entry>& pitEntry)
219 NFD_LOG_DEBUG(
"afterReceiveNack in=" << ingress <<
" pitEntry=" << pitEntry->getName());
225 NFD_LOG_DEBUG(
"onDroppedInterest out=" << egress.
getId() <<
" name=" << interest.getName());
231 NFD_LOG_DEBUG(
"afterNewNextHop pitEntry=" << pitEntry->getName()
238 if (interest.getTag<lp::PitToken>() !=
nullptr) {
239 Interest interest2 = interest;
240 interest2.removeTag<lp::PitToken>();
241 return m_forwarder.onOutgoingInterest(interest2, egress, pitEntry);
243 return m_forwarder.onOutgoingInterest(interest, egress, pitEntry);
249 BOOST_ASSERT(pitEntry->getInterest().matchesData(data));
251 auto inRecord = pitEntry->findInRecord(egress);
252 if (inRecord != pitEntry->in_end()) {
253 auto pitToken = inRecord->getInterest().getTag<lp::PitToken>();
257 pitEntry->deleteInRecord(inRecord);
259 if (pitToken !=
nullptr) {
261 data2.setTag(pitToken);
262 return m_forwarder.onOutgoingData(data2, egress);
265 return m_forwarder.onOutgoingData(data, egress);
271 std::set<Face*> pendingDownstreams;
272 auto now = time::steady_clock::now();
275 for (
const auto& inRecord : pitEntry->getInRecords()) {
276 if (inRecord.getExpiry() > now) {
277 if (inRecord.getFace().getId() == inFace.
getId() &&
278 inRecord.getFace().getLinkType() != ndn::nfd::LINK_TYPE_AD_HOC) {
281 pendingDownstreams.emplace(&inRecord.getFace());
285 for (
const auto& pendingDownstream : pendingDownstreams) {
286 this->
sendData(data, *pendingDownstream, pitEntry);
292 std::initializer_list<const Face*> exceptFaces)
295 std::unordered_set<Face*> downstreams;
296 std::transform(pitEntry->in_begin(), pitEntry->in_end(),
297 std::inserter(downstreams, downstreams.end()),
298 [] (
const auto& inR) { return &inR.getFace(); });
301 for (
auto exceptFace : exceptFaces) {
302 downstreams.erase(
const_cast<Face*
>(exceptFace));
306 for (
auto downstream : downstreams) {
307 this->
sendNack(header, *downstream, pitEntry);
319 if (interest.getForwardingHint().empty()) {
326 const auto& fh = interest.getForwardingHint();
331 for (
const auto& delegation : fh) {
344 BOOST_ASSERT(fibEntry->
getPrefix().empty());
346 BOOST_ASSERT(fibEntry !=
nullptr && fibEntry->
getPrefix().empty());
Represents a face-endpoint pair in the forwarder.
Main class of NFD's forwarding engine.
NetworkRegionTable & getNetworkRegionTable() noexcept
bool isInProducerRegion(span< const Name > forwardingHint) const
Determines whether an Interest has reached a producer region.
Generalization of a network interface.
FaceId getId() const noexcept
Returns the face ID.
Represents an entry in the FIB.
bool hasNextHops() const noexcept
Returns whether this Entry has any NextHop records.
const Name & getPrefix() const noexcept
Represents the Forwarding Information Base (FIB).
const Entry & findLongestPrefixMatch(const Name &prefix) const
Performs a longest prefix match.
Represents a nexthop record in a FIB entry.
Face & getFace() const noexcept
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.
void sendNacks(const lp::NackHeader &header, const shared_ptr< pit::Entry > &pitEntry, std::initializer_list< const Face * > exceptFaces={})
Send Nack to every face that has an in-record, except those in exceptFaces.
Strategy(Forwarder &forwarder)
Construct a strategy instance.
static bool canCreate(const Name &instanceName)
Returns whether a strategy instance can be created from instanceName.
const fib::Entry & lookupFib(const pit::Entry &pitEntry) const
Performs a FIB lookup, considering Link object if present.
bool sendNack(const lp::NackHeader &header, Face &egress, const shared_ptr< pit::Entry > &pitEntry)
Send a Nack packet.
virtual void afterNewNextHop(const fib::NextHop &nextHop, const shared_ptr< pit::Entry > &pitEntry)
Trigger after a new nexthop is added.
pit::OutRecord * sendInterest(const Interest &interest, Face &egress, const shared_ptr< pit::Entry > &pitEntry)
Send an Interest packet.
bool sendData(const Data &data, Face &egress, const shared_ptr< pit::Entry > &pitEntry)
Send a Data packet.
static ParsedInstanceName parseInstanceName(const Name &input)
Parse a strategy instance name.
void sendDataToAll(const Data &data, const shared_ptr< pit::Entry > &pitEntry, const Face &inFace)
Send a Data packet to all matched and qualified faces.
virtual void onInterestLoop(const Interest &interest, const FaceEndpoint &ingress)
Trigger after an Interest loop is detected.
virtual void afterReceiveData(const Data &data, const FaceEndpoint &ingress, const shared_ptr< pit::Entry > &pitEntry)
Trigger after Data is received.
static std::set< Name > listRegistered()
Returns all registered versioned strategy names.
static StrategyParameters parseParameters(const PartialName ¶ms)
Parse strategy parameters encoded in a strategy instance name.
virtual void beforeSatisfyInterest(const Data &data, const FaceEndpoint &ingress, const shared_ptr< pit::Entry > &pitEntry)
Trigger before a PIT entry is satisfied.
static Name makeInstanceName(const Name &input, const Name &strategyName)
Construct a strategy instance name.
static unique_ptr< Strategy > create(const Name &instanceName, Forwarder &forwarder)
Returns a strategy instance created from instanceName.
static bool areSameType(const Name &instanceNameA, const Name &instanceNameB)
Returns whether two names will instantiate the same strategy type.
Represents an entry in the Interest table (PIT).
const Interest & getInterest() const
Contains information about an Interest toward an outgoing face.
#define NFD_LOG_INIT(name)