strategy.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014-2020, Regents of the University of California,
4  * Arizona Board of Regents,
5  * Colorado State University,
6  * University Pierre & Marie Curie, Sorbonne University,
7  * Washington University in St. Louis,
8  * Beijing Institute of Technology,
9  * The University of Memphis.
10  *
11  * This file is part of NFD (Named Data Networking Forwarding Daemon).
12  * See AUTHORS.md for complete list of NFD authors and contributors.
13  *
14  * NFD is free software: you can redistribute it and/or modify it under the terms
15  * of the GNU General Public License as published by the Free Software Foundation,
16  * either version 3 of the License, or (at your option) any later version.
17  *
18  * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
19  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
20  * PURPOSE. See the GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License along with
23  * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
24  */
25 
26 #include "strategy.hpp"
27 #include "forwarder.hpp"
28 #include "common/logger.hpp"
29 
30 #include <ndn-cxx/lp/pit-token.hpp>
31 
32 #include <boost/range/adaptor/map.hpp>
33 #include <boost/range/algorithm/copy.hpp>
34 
35 namespace nfd {
36 namespace fw {
37 
38 NFD_LOG_INIT(Strategy);
39 
40 Strategy::Registry&
41 Strategy::getRegistry()
42 {
43  static Registry registry;
44  return registry;
45 }
46 
47 Strategy::Registry::const_iterator
48 Strategy::find(const Name& instanceName)
49 {
50  const Registry& registry = getRegistry();
51  ParsedInstanceName parsed = parseInstanceName(instanceName);
52 
53  if (parsed.version) {
54  // specified version: find exact or next higher version
55 
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);
60  return found;
61  }
62  }
63 
64  NFD_LOG_TRACE("find " << instanceName << " versioned not-found");
65  return registry.end();
66  }
67 
68  // no version specified: find highest version
69 
70  if (!parsed.strategyName.empty()) { // Name().getSuccessor() would be invalid
71  auto found = registry.lower_bound(parsed.strategyName.getSuccessor());
72  if (found != registry.begin()) {
73  --found;
74  if (parsed.strategyName.isPrefixOf(found->first)) {
75  NFD_LOG_TRACE("find " << instanceName << " unversioned found=" << found->first);
76  return found;
77  }
78  }
79  }
80 
81  NFD_LOG_TRACE("find " << instanceName << " unversioned not-found");
82  return registry.end();
83 }
84 
85 bool
86 Strategy::canCreate(const Name& instanceName)
87 {
88  return Strategy::find(instanceName) != getRegistry().end();
89 }
90 
91 unique_ptr<Strategy>
92 Strategy::create(const Name& instanceName, Forwarder& forwarder)
93 {
94  auto found = Strategy::find(instanceName);
95  if (found == getRegistry().end()) {
96  NFD_LOG_DEBUG("create " << instanceName << " not-found");
97  return nullptr;
98  }
99 
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());
104  return instance;
105 }
106 
107 bool
108 Strategy::areSameType(const Name& instanceNameA, const Name& instanceNameB)
109 {
110  return Strategy::find(instanceNameA) == Strategy::find(instanceNameB);
111 }
112 
113 std::set<Name>
115 {
116  std::set<Name> strategyNames;
117  boost::copy(getRegistry() | boost::adaptors::map_keys,
118  std::inserter(strategyNames, strategyNames.end()));
119  return strategyNames;
120 }
121 
123 Strategy::parseInstanceName(const Name& input)
124 {
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)};
128  }
129  }
130  return {input, nullopt, PartialName()};
131 }
132 
133 Name
134 Strategy::makeInstanceName(const Name& input, const Name& strategyName)
135 {
136  BOOST_ASSERT(strategyName.at(-1).isVersion());
137 
138  bool hasVersion = std::any_of(input.rbegin(), input.rend(),
139  [] (const name::Component& comp) { return comp.isVersion(); });
140  return hasVersion ? input : Name(input).append(strategyName.at(-1));
141 }
142 
144  : afterAddFace(forwarder.m_faceTable.afterAdd)
145  , beforeRemoveFace(forwarder.m_faceTable.beforeRemove)
146  , m_forwarder(forwarder)
147  , m_measurements(m_forwarder.getMeasurements(), m_forwarder.getStrategyChoice(), *this)
148 {
149 }
150 
151 Strategy::~Strategy() = default;
152 
153 void
154 Strategy::beforeSatisfyInterest(const shared_ptr<pit::Entry>& pitEntry,
155  const FaceEndpoint& ingress, const Data& data)
156 {
157  NFD_LOG_DEBUG("beforeSatisfyInterest pitEntry=" << pitEntry->getName()
158  << " in=" << ingress << " data=" << data.getName());
159 }
160 
161 void
162 Strategy::afterContentStoreHit(const shared_ptr<pit::Entry>& pitEntry,
163  const FaceEndpoint& ingress, const Data& data)
164 {
165  NFD_LOG_DEBUG("afterContentStoreHit pitEntry=" << pitEntry->getName()
166  << " in=" << ingress << " data=" << data.getName());
167 
168  this->sendData(pitEntry, data, ingress.face);
169 }
170 
171 void
172 Strategy::afterReceiveData(const shared_ptr<pit::Entry>& pitEntry,
173  const FaceEndpoint& ingress, const Data& data)
174 {
175  NFD_LOG_DEBUG("afterReceiveData pitEntry=" << pitEntry->getName()
176  << " in=" << ingress << " data=" << data.getName());
177 
178  this->beforeSatisfyInterest(pitEntry, ingress, data);
179 
180  this->sendDataToAll(pitEntry, ingress.face, data);
181 }
182 
183 void
184 Strategy::afterReceiveNack(const FaceEndpoint& ingress, const lp::Nack&,
185  const shared_ptr<pit::Entry>& pitEntry)
186 {
187  NFD_LOG_DEBUG("afterReceiveNack in=" << ingress << " pitEntry=" << pitEntry->getName());
188 }
189 
190 void
191 Strategy::onDroppedInterest(const Face& egress, const Interest& interest)
192 {
193  NFD_LOG_DEBUG("onDroppedInterest out=" << egress.getId() << " name=" << interest.getName());
194 }
195 
197 Strategy::sendInterest(const shared_ptr<pit::Entry>& pitEntry, Face& egress, const Interest& interest)
198 {
199  if (interest.getTag<lp::PitToken>() != nullptr) {
200  Interest interest2 = interest; // make a copy to preserve tag on original packet
201  interest2.removeTag<lp::PitToken>();
202  return m_forwarder.onOutgoingInterest(pitEntry, egress, interest2);
203  }
204  return m_forwarder.onOutgoingInterest(pitEntry, egress, interest);
205 }
206 
207 void
208 Strategy::afterNewNextHop(const fib::NextHop& nextHop, const shared_ptr<pit::Entry>& pitEntry)
209 {
210  NFD_LOG_DEBUG("afterNewNextHop pitEntry=" << pitEntry->getName()
211  << " nexthop=" << nextHop.getFace().getId());
212 }
213 
214 bool
215 Strategy::sendData(const shared_ptr<pit::Entry>& pitEntry, const Data& data, Face& egress)
216 {
217  BOOST_ASSERT(pitEntry->getInterest().matchesData(data));
218 
219  shared_ptr<lp::PitToken> pitToken;
220  auto inRecord = pitEntry->getInRecord(egress);
221  if (inRecord != pitEntry->in_end()) {
222  pitToken = inRecord->getInterest().getTag<lp::PitToken>();
223  }
224 
225  // delete the PIT entry's in-record based on egress,
226  // since the Data is sent to the face from which the Interest was received
227  pitEntry->deleteInRecord(egress);
228 
229  if (pitToken != nullptr) {
230  Data data2 = data; // make a copy so each downstream can get a different PIT token
231  data2.setTag(pitToken);
232  return m_forwarder.onOutgoingData(data2, egress);
233  }
234  return m_forwarder.onOutgoingData(data, egress);
235 }
236 
237 void
238 Strategy::sendDataToAll(const shared_ptr<pit::Entry>& pitEntry,
239  const Face& inFace, const Data& data)
240 {
241  std::set<Face*> pendingDownstreams;
242  auto now = time::steady_clock::now();
243 
244  // remember pending downstreams
245  for (const pit::InRecord& inRecord : pitEntry->getInRecords()) {
246  if (inRecord.getExpiry() > now) {
247  if (inRecord.getFace().getId() == inFace.getId() &&
248  inRecord.getFace().getLinkType() != ndn::nfd::LINK_TYPE_AD_HOC) {
249  continue;
250  }
251  pendingDownstreams.emplace(&inRecord.getFace());
252  }
253  }
254 
255  for (const auto& pendingDownstream : pendingDownstreams) {
256  this->sendData(pitEntry, data, *pendingDownstream);
257  }
258 }
259 
260 void
261 Strategy::sendNacks(const shared_ptr<pit::Entry>& pitEntry, const lp::NackHeader& header,
262  std::initializer_list<const Face*> exceptFaces)
263 {
264  // populate downstreams with all downstreams faces
265  std::unordered_set<Face*> downstreams;
266  std::transform(pitEntry->in_begin(), pitEntry->in_end(), std::inserter(downstreams, downstreams.end()),
267  [] (const pit::InRecord& inR) { return &inR.getFace(); });
268 
269  // delete excluded faces
270  for (auto exceptFace : exceptFaces) {
271  downstreams.erase(const_cast<Face*>(exceptFace));
272  }
273 
274  // send Nacks
275  for (auto downstream : downstreams) {
276  this->sendNack(pitEntry, *downstream, header);
277  }
278  // warning: don't loop on pitEntry->getInRecords(), because in-record is deleted when sending Nack
279 }
280 
281 const fib::Entry&
282 Strategy::lookupFib(const pit::Entry& pitEntry) const
283 {
284  const Fib& fib = m_forwarder.getFib();
285 
286  const Interest& interest = pitEntry.getInterest();
287  // has forwarding hint?
288  if (interest.getForwardingHint().empty()) {
289  // FIB lookup with Interest name
290  const fib::Entry& fibEntry = fib.findLongestPrefixMatch(pitEntry);
291  NFD_LOG_TRACE("lookupFib noForwardingHint found=" << fibEntry.getPrefix());
292  return fibEntry;
293  }
294 
295  const DelegationList& fh = interest.getForwardingHint();
296  // Forwarding hint should have been stripped by incoming Interest pipeline when reaching producer region
297  BOOST_ASSERT(!m_forwarder.getNetworkRegionTable().isInProducerRegion(fh));
298 
299  const fib::Entry* fibEntry = nullptr;
300  for (const Delegation& del : fh) {
301  fibEntry = &fib.findLongestPrefixMatch(del.name);
302  if (fibEntry->hasNextHops()) {
303  if (fibEntry->getPrefix().size() == 0) {
304  // in consumer region, return the default route
305  NFD_LOG_TRACE("lookupFib inConsumerRegion found=" << fibEntry->getPrefix());
306  }
307  else {
308  // in default-free zone, use the first delegation that finds a FIB entry
309  NFD_LOG_TRACE("lookupFib delegation=" << del.name << " found=" << fibEntry->getPrefix());
310  }
311  return *fibEntry;
312  }
313  BOOST_ASSERT(fibEntry->getPrefix().size() == 0); // only ndn:/ FIB entry can have zero nexthop
314  }
315  BOOST_ASSERT(fibEntry != nullptr && fibEntry->getPrefix().size() == 0);
316  return *fibEntry; // only occurs if no delegation finds a FIB nexthop
317 }
318 
319 } // namespace fw
320 } // namespace nfd
virtual void afterNewNextHop(const fib::NextHop &nextHop, const shared_ptr< pit::Entry > &pitEntry)
Trigger after new nexthop is added.
Definition: strategy.cpp:208
Main class of NFD&#39;s forwarding engine.
Definition: forwarder.hpp:51
Strategy(Forwarder &forwarder)
Construct a strategy instance.
Definition: strategy.cpp:143
bool sendNack(const shared_ptr< pit::Entry > &pitEntry, Face &egress, const lp::NackHeader &header)
Send a Nack packet.
Definition: strategy.hpp:306
Face & getFace() const
Definition: fib-nexthop.hpp:47
Contains information about an Interest toward an outgoing face.
Fib & getFib()
Definition: forwarder.hpp:127
bool sendData(const shared_ptr< pit::Entry > &pitEntry, const Data &data, Face &egress)
Send a Data packet.
Definition: strategy.cpp:215
represents a FIB entry
Definition: fib-entry.hpp:53
signal::Signal< FaceTable, Face > & beforeRemoveFace
Definition: strategy.hpp:412
#define NFD_LOG_TRACE
Definition: logger.hpp:37
static Name makeInstanceName(const Name &input, const Name &strategyName)
Construct a strategy instance name.
Definition: strategy.cpp:134
static bool canCreate(const Name &instanceName)
Definition: strategy.cpp:86
bool isInProducerRegion(const DelegationList &forwardingHint) const
determines whether an Interest has reached a producer region
Represents the Forwarding Information Base (FIB)
Definition: fib.hpp:47
const fib::Entry & lookupFib(const pit::Entry &pitEntry) const
Performs a FIB lookup, considering Link object if present.
Definition: strategy.cpp:282
MeasurementsAccessor & getMeasurements()
Definition: strategy.hpp:337
virtual void afterReceiveNack(const FaceEndpoint &ingress, const lp::Nack &nack, const shared_ptr< pit::Entry > &pitEntry)
Trigger after Nack is received.
Definition: strategy.cpp:184
static bool areSameType(const Name &instanceNameA, const Name &instanceNameB)
Definition: strategy.cpp:108
const Interest & getInterest() const
Definition: pit-entry.hpp:70
An Interest table entry.
Definition: pit-entry.hpp:58
Represents a face-endpoint pair in the forwarder.
Copyright (c) 2014-2015, Regents of the University of California, Arizona Board of Regents...
Definition: algorithm.hpp:32
virtual void onDroppedInterest(const Face &egress, const Interest &interest)
Trigger after Interest dropped (e.g., for exceeding allowed retransmissions)
Definition: strategy.cpp:191
Contains information about an Interest from an incoming face.
const Name & getPrefix() const
Definition: fib-entry.hpp:60
void sendDataToAll(const shared_ptr< pit::Entry > &pitEntry, const Face &inFace, const Data &data)
Send a Data packet to all matched and qualified faces.
Definition: strategy.cpp:238
const Entry & findLongestPrefixMatch(const Name &prefix) const
Performs a longest prefix match.
Definition: fib.cpp:62
generalization of a network interface
Definition: face.hpp:54
void sendNacks(const shared_ptr< pit::Entry > &pitEntry, const lp::NackHeader &header, std::initializer_list< const Face *> exceptFaces={})
Send Nack to every face that has an in-record, except those in exceptFaces.
Definition: strategy.cpp:261
Face & getFace() const
FaceId getId() const
Definition: face.hpp:239
static unique_ptr< Strategy > create(const Name &instanceName, Forwarder &forwarder)
Definition: strategy.cpp:92
virtual void afterContentStoreHit(const shared_ptr< pit::Entry > &pitEntry, const FaceEndpoint &ingress, const Data &data)
Trigger after a Data is matched in CS.
Definition: strategy.cpp:162
#define NFD_LOG_DEBUG
Definition: logger.hpp:38
#define NFD_LOG_INIT(name)
Definition: logger.hpp:31
virtual void afterReceiveData(const shared_ptr< pit::Entry > &pitEntry, const FaceEndpoint &ingress, const Data &data)
Trigger after Data is received.
Definition: strategy.cpp:172
static ParsedInstanceName parseInstanceName(const Name &input)
Parse a strategy instance name.
Definition: strategy.cpp:123
static std::set< Name > listRegistered()
Definition: strategy.cpp:114
signal::Signal< FaceTable, Face > & afterAddFace
Definition: strategy.hpp:411
Represents a nexthop record in a FIB entry.
Definition: fib-nexthop.hpp:37
virtual ~Strategy()
virtual void beforeSatisfyInterest(const shared_ptr< pit::Entry > &pitEntry, const FaceEndpoint &ingress, const Data &data)
Trigger before PIT entry is satisfied.
Definition: strategy.cpp:154
NetworkRegionTable & getNetworkRegionTable()
Definition: forwarder.hpp:163
pit::OutRecord * sendInterest(const shared_ptr< pit::Entry > &pitEntry, Face &egress, const Interest &interest)
Send an Interest packet.
Definition: strategy.cpp:197