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-2022, 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 auto& 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::afterContentStoreHit(const Data& data, const FaceEndpoint& ingress,
155  const shared_ptr<pit::Entry>& pitEntry)
156 {
157  NFD_LOG_DEBUG("afterContentStoreHit pitEntry=" << pitEntry->getName()
158  << " in=" << ingress << " data=" << data.getName());
159 
160  this->sendData(data, ingress.face, pitEntry);
161 }
162 
163 void
164 Strategy::beforeSatisfyInterest(const Data& data, const FaceEndpoint& ingress,
165  const shared_ptr<pit::Entry>& pitEntry)
166 {
167  NFD_LOG_DEBUG("beforeSatisfyInterest pitEntry=" << pitEntry->getName()
168  << " in=" << ingress << " data=" << data.getName());
169 }
170 
171 void
172 Strategy::afterReceiveData(const Data& data, const FaceEndpoint& ingress,
173  const shared_ptr<pit::Entry>& pitEntry)
174 {
175  NFD_LOG_DEBUG("afterReceiveData pitEntry=" << pitEntry->getName()
176  << " in=" << ingress << " data=" << data.getName());
177 
178  this->beforeSatisfyInterest(data, ingress, pitEntry);
179  this->sendDataToAll(data, pitEntry, ingress.face);
180 }
181 
182 void
183 Strategy::afterReceiveNack(const lp::Nack&, const FaceEndpoint& ingress,
184  const shared_ptr<pit::Entry>& pitEntry)
185 {
186  NFD_LOG_DEBUG("afterReceiveNack in=" << ingress << " pitEntry=" << pitEntry->getName());
187 }
188 
189 void
190 Strategy::onDroppedInterest(const Interest& interest, Face& egress)
191 {
192  NFD_LOG_DEBUG("onDroppedInterest out=" << egress.getId() << " name=" << interest.getName());
193 }
194 
195 void
196 Strategy::afterNewNextHop(const fib::NextHop& nextHop, const shared_ptr<pit::Entry>& pitEntry)
197 {
198  NFD_LOG_DEBUG("afterNewNextHop pitEntry=" << pitEntry->getName()
199  << " nexthop=" << nextHop.getFace().getId());
200 }
201 
203 Strategy::sendInterest(const Interest& interest, Face& egress, const shared_ptr<pit::Entry>& pitEntry)
204 {
205  if (interest.getTag<lp::PitToken>() != nullptr) {
206  Interest interest2 = interest; // make a copy to preserve tag on original packet
207  interest2.removeTag<lp::PitToken>();
208  return m_forwarder.onOutgoingInterest(interest2, egress, pitEntry);
209  }
210  return m_forwarder.onOutgoingInterest(interest, egress, pitEntry);
211 }
212 
213 bool
214 Strategy::sendData(const Data& data, Face& egress, const shared_ptr<pit::Entry>& pitEntry)
215 {
216  BOOST_ASSERT(pitEntry->getInterest().matchesData(data));
217 
218  shared_ptr<lp::PitToken> pitToken;
219  auto inRecord = pitEntry->getInRecord(egress);
220  if (inRecord != pitEntry->in_end()) {
221  pitToken = inRecord->getInterest().getTag<lp::PitToken>();
222  }
223 
224  // delete the PIT entry's in-record based on egress,
225  // since the Data is sent to the face from which the Interest was received
226  pitEntry->deleteInRecord(egress);
227 
228  if (pitToken != nullptr) {
229  Data data2 = data; // make a copy so each downstream can get a different PIT token
230  data2.setTag(pitToken);
231  return m_forwarder.onOutgoingData(data2, egress);
232  }
233  return m_forwarder.onOutgoingData(data, egress);
234 }
235 
236 void
237 Strategy::sendDataToAll(const Data& data, const shared_ptr<pit::Entry>& pitEntry, const Face& inFace)
238 {
239  std::set<Face*> pendingDownstreams;
240  auto now = time::steady_clock::now();
241 
242  // remember pending downstreams
243  for (const auto& inRecord : pitEntry->getInRecords()) {
244  if (inRecord.getExpiry() > now) {
245  if (inRecord.getFace().getId() == inFace.getId() &&
246  inRecord.getFace().getLinkType() != ndn::nfd::LINK_TYPE_AD_HOC) {
247  continue;
248  }
249  pendingDownstreams.emplace(&inRecord.getFace());
250  }
251  }
252 
253  for (const auto& pendingDownstream : pendingDownstreams) {
254  this->sendData(data, *pendingDownstream, pitEntry);
255  }
256 }
257 
258 void
259 Strategy::sendNacks(const lp::NackHeader& header, const shared_ptr<pit::Entry>& pitEntry,
260  std::initializer_list<const Face*> exceptFaces)
261 {
262  // populate downstreams with all downstreams faces
263  std::unordered_set<Face*> downstreams;
264  std::transform(pitEntry->in_begin(), pitEntry->in_end(),
265  std::inserter(downstreams, downstreams.end()),
266  [] (const auto& inR) { return &inR.getFace(); });
267 
268  // remove excluded faces
269  for (auto exceptFace : exceptFaces) {
270  downstreams.erase(const_cast<Face*>(exceptFace));
271  }
272 
273  // send Nacks
274  for (auto downstream : downstreams) {
275  this->sendNack(header, *downstream, pitEntry);
276  }
277  // warning: don't loop on pitEntry->getInRecords(), because in-record is deleted when sending Nack
278 }
279 
280 const fib::Entry&
281 Strategy::lookupFib(const pit::Entry& pitEntry) const
282 {
283  const Fib& fib = m_forwarder.getFib();
284 
285  const Interest& interest = pitEntry.getInterest();
286  // has forwarding hint?
287  if (interest.getForwardingHint().empty()) {
288  // FIB lookup with Interest name
289  const fib::Entry& fibEntry = fib.findLongestPrefixMatch(pitEntry);
290  NFD_LOG_TRACE("lookupFib noForwardingHint found=" << fibEntry.getPrefix());
291  return fibEntry;
292  }
293 
294  const auto& fh = interest.getForwardingHint();
295  // Forwarding hint should have been stripped by incoming Interest pipeline when reaching producer region
296  BOOST_ASSERT(!m_forwarder.getNetworkRegionTable().isInProducerRegion(fh));
297 
298  const fib::Entry* fibEntry = nullptr;
299  for (const auto& delegation : fh) {
300  fibEntry = &fib.findLongestPrefixMatch(delegation);
301  if (fibEntry->hasNextHops()) {
302  if (fibEntry->getPrefix().size() == 0) {
303  // in consumer region, return the default route
304  NFD_LOG_TRACE("lookupFib inConsumerRegion found=" << fibEntry->getPrefix());
305  }
306  else {
307  // in default-free zone, use the first delegation that finds a FIB entry
308  NFD_LOG_TRACE("lookupFib delegation=" << delegation << " found=" << fibEntry->getPrefix());
309  }
310  return *fibEntry;
311  }
312  BOOST_ASSERT(fibEntry->getPrefix().size() == 0); // only ndn:/ FIB entry can have zero nexthop
313  }
314  BOOST_ASSERT(fibEntry != nullptr && fibEntry->getPrefix().size() == 0);
315  return *fibEntry; // only occurs if no delegation finds a FIB nexthop
316 }
317 
318 } // namespace fw
319 } // namespace nfd
Represents a face-endpoint pair in the forwarder.
Main class of NFD's forwarding engine.
Definition: forwarder.hpp:54
Fib & getFib()
Definition: forwarder.hpp:88
NetworkRegionTable & getNetworkRegionTable()
Definition: forwarder.hpp:124
bool isInProducerRegion(span< const Name > forwardingHint) const
determines whether an Interest has reached a producer region
generalization of a network interface
Definition: face.hpp:55
FaceId getId() const
Definition: face.hpp:248
represents a FIB entry
Definition: fib-entry.hpp:54
bool hasNextHops() const
Definition: fib-entry.hpp:74
const Name & getPrefix() const
Definition: fib-entry.hpp:60
Represents the Forwarding Information Base (FIB)
Definition: fib.hpp:48
const Entry & findLongestPrefixMatch(const Name &prefix) const
Performs a longest prefix match.
Definition: fib.cpp:62
Represents a nexthop record in a FIB entry.
Definition: fib-nexthop.hpp:38
Face & getFace() const
Definition: fib-nexthop.hpp:47
virtual void onDroppedInterest(const Interest &interest, Face &egress)
Trigger after an Interest is dropped (e.g., for exceeding allowed retransmissions).
Definition: strategy.cpp:190
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.
Definition: strategy.cpp:154
virtual void afterReceiveNack(const lp::Nack &nack, const FaceEndpoint &ingress, const shared_ptr< pit::Entry > &pitEntry)
Trigger after a Nack is received.
Definition: strategy.cpp:183
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.
Definition: strategy.cpp:259
Strategy(Forwarder &forwarder)
Construct a strategy instance.
Definition: strategy.cpp:143
static bool canCreate(const Name &instanceName)
Definition: strategy.cpp:86
const fib::Entry & lookupFib(const pit::Entry &pitEntry) const
Performs a FIB lookup, considering Link object if present.
Definition: strategy.cpp:281
bool sendNack(const lp::NackHeader &header, Face &egress, const shared_ptr< pit::Entry > &pitEntry)
Send a Nack packet.
Definition: strategy.hpp:324
virtual void afterNewNextHop(const fib::NextHop &nextHop, const shared_ptr< pit::Entry > &pitEntry)
Trigger after a new nexthop is added.
Definition: strategy.cpp:196
pit::OutRecord * sendInterest(const Interest &interest, Face &egress, const shared_ptr< pit::Entry > &pitEntry)
Send an Interest packet.
Definition: strategy.cpp:203
bool sendData(const Data &data, Face &egress, const shared_ptr< pit::Entry > &pitEntry)
Send a Data packet.
Definition: strategy.cpp:214
static ParsedInstanceName parseInstanceName(const Name &input)
Parse a strategy instance name.
Definition: strategy.cpp:123
void sendDataToAll(const Data &data, const shared_ptr< pit::Entry > &pitEntry, const Face &inFace)
Send a Data packet to all matched and qualified faces.
Definition: strategy.cpp:237
virtual ~Strategy()
virtual void afterReceiveData(const Data &data, const FaceEndpoint &ingress, const shared_ptr< pit::Entry > &pitEntry)
Trigger after Data is received.
Definition: strategy.cpp:172
static std::set< Name > listRegistered()
Definition: strategy.cpp:114
virtual void beforeSatisfyInterest(const Data &data, const FaceEndpoint &ingress, const shared_ptr< pit::Entry > &pitEntry)
Trigger before a PIT entry is satisfied.
Definition: strategy.cpp:164
static Name makeInstanceName(const Name &input, const Name &strategyName)
Construct a strategy instance name.
Definition: strategy.cpp:134
static unique_ptr< Strategy > create(const Name &instanceName, Forwarder &forwarder)
Definition: strategy.cpp:92
static bool areSameType(const Name &instanceNameA, const Name &instanceNameB)
Definition: strategy.cpp:108
An Interest table entry.
Definition: pit-entry.hpp:59
const Interest & getInterest() const
Definition: pit-entry.hpp:70
Contains information about an Interest toward an outgoing face.
#define NFD_LOG_INIT(name)
Definition: logger.hpp:31
#define NFD_LOG_DEBUG
Definition: logger.hpp:38
#define NFD_LOG_TRACE
Definition: logger.hpp:37
Copyright (c) 2014-2015, Regents of the University of California, Arizona Board of Regents,...
Definition: algorithm.hpp:32