blob: 4dfe32e30c3bf0a1642c13a30b14a67ca76e2885 [file] [log] [blame]
Junxiao Shid3c792f2014-01-30 00:46:13 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shifc2e13d2017-07-25 02:08:48 +00002/*
Teng Liang63086442018-02-25 20:39:42 -07003 * Copyright (c) 2014-2018, Regents of the University of California,
Junxiao Shi35353962015-01-08 09:13:47 -07004 * 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.
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070010 *
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/>.
Junxiao Shi82e7f582014-09-07 15:15:40 -070024 */
Junxiao Shid3c792f2014-01-30 00:46:13 -070025
26#include "strategy.hpp"
Junxiao Shi8c8d2182014-01-30 22:33:00 -070027#include "forwarder.hpp"
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060028#include "core/logger.hpp"
Junxiao Shic5f651f2016-11-17 22:58:12 +000029#include "core/random.hpp"
Davide Pesaventoa3148082018-04-12 18:21:54 -040030
Junxiao Shic34d1672016-12-09 15:57:59 +000031#include <boost/range/adaptor/map.hpp>
32#include <boost/range/algorithm/copy.hpp>
Junxiao Shid3c792f2014-01-30 00:46:13 -070033
34namespace nfd {
Junxiao Shi8c8d2182014-01-30 22:33:00 -070035namespace fw {
Junxiao Shid3c792f2014-01-30 00:46:13 -070036
Davide Pesaventoa3148082018-04-12 18:21:54 -040037NFD_LOG_INIT(Strategy);
Junxiao Shi679e9272014-02-15 20:10:21 -070038
Junxiao Shic34d1672016-12-09 15:57:59 +000039Strategy::Registry&
40Strategy::getRegistry()
41{
42 static Registry registry;
43 return registry;
44}
45
46Strategy::Registry::const_iterator
Junxiao Shi18739c42016-12-22 08:03:00 +000047Strategy::find(const Name& instanceName)
Junxiao Shic34d1672016-12-09 15:57:59 +000048{
49 const Registry& registry = getRegistry();
Junxiao Shi18739c42016-12-22 08:03:00 +000050 ParsedInstanceName parsed = parseInstanceName(instanceName);
51
Junxiao Shi18739c42016-12-22 08:03:00 +000052 if (parsed.version) {
Junxiao Shi91f6ee02016-12-29 21:44:44 +000053 // specified version: find exact or next higher version
54
55 auto found = registry.lower_bound(parsed.strategyName);
56 if (found != registry.end()) {
57 if (parsed.strategyName.getPrefix(-1).isPrefixOf(found->first)) {
58 NFD_LOG_TRACE("find " << instanceName << " versioned found=" << found->first);
59 return found;
60 }
61 }
62
63 NFD_LOG_TRACE("find " << instanceName << " versioned not-found");
64 return registry.end();
Junxiao Shi18739c42016-12-22 08:03:00 +000065 }
66
Junxiao Shi91f6ee02016-12-29 21:44:44 +000067 // no version specified: find highest version
68
69 if (!parsed.strategyName.empty()) { // Name().getSuccessor() would be invalid
70 auto found = registry.lower_bound(parsed.strategyName.getSuccessor());
71 if (found != registry.begin()) {
72 --found;
73 if (parsed.strategyName.isPrefixOf(found->first)) {
74 NFD_LOG_TRACE("find " << instanceName << " unversioned found=" << found->first);
75 return found;
76 }
Junxiao Shic34d1672016-12-09 15:57:59 +000077 }
78 }
Junxiao Shi91f6ee02016-12-29 21:44:44 +000079
80 NFD_LOG_TRACE("find " << instanceName << " unversioned not-found");
81 return registry.end();
Junxiao Shic34d1672016-12-09 15:57:59 +000082}
83
84bool
Junxiao Shi18739c42016-12-22 08:03:00 +000085Strategy::canCreate(const Name& instanceName)
Junxiao Shic34d1672016-12-09 15:57:59 +000086{
Junxiao Shi18739c42016-12-22 08:03:00 +000087 return Strategy::find(instanceName) != getRegistry().end();
Junxiao Shic34d1672016-12-09 15:57:59 +000088}
89
90unique_ptr<Strategy>
Junxiao Shi18739c42016-12-22 08:03:00 +000091Strategy::create(const Name& instanceName, Forwarder& forwarder)
Junxiao Shic34d1672016-12-09 15:57:59 +000092{
Junxiao Shi18739c42016-12-22 08:03:00 +000093 auto found = Strategy::find(instanceName);
Junxiao Shic34d1672016-12-09 15:57:59 +000094 if (found == getRegistry().end()) {
Junxiao Shi18739c42016-12-22 08:03:00 +000095 NFD_LOG_DEBUG("create " << instanceName << " not-found");
Junxiao Shic34d1672016-12-09 15:57:59 +000096 return nullptr;
97 }
98
Junxiao Shi18739c42016-12-22 08:03:00 +000099 unique_ptr<Strategy> instance = found->second(forwarder, instanceName);
100 NFD_LOG_DEBUG("create " << instanceName << " found=" << found->first <<
101 " created=" << instance->getInstanceName());
102 BOOST_ASSERT(!instance->getInstanceName().empty());
103 return instance;
Junxiao Shic34d1672016-12-09 15:57:59 +0000104}
105
Junxiao Shi55e21b92017-01-23 03:27:47 +0000106bool
107Strategy::areSameType(const Name& instanceNameA, const Name& instanceNameB)
108{
109 return Strategy::find(instanceNameA) == Strategy::find(instanceNameB);
110}
111
Junxiao Shic34d1672016-12-09 15:57:59 +0000112std::set<Name>
113Strategy::listRegistered()
114{
115 std::set<Name> strategyNames;
116 boost::copy(getRegistry() | boost::adaptors::map_keys,
117 std::inserter(strategyNames, strategyNames.end()));
118 return strategyNames;
119}
120
Junxiao Shi18739c42016-12-22 08:03:00 +0000121Strategy::ParsedInstanceName
122Strategy::parseInstanceName(const Name& input)
123{
124 for (ssize_t i = input.size() - 1; i > 0; --i) {
125 if (input[i].isVersion()) {
126 return {input.getPrefix(i + 1), input[i].toVersion(), input.getSubName(i + 1)};
127 }
128 }
Davide Pesavento87fc0f82018-04-11 23:43:51 -0400129 return {input, nullopt, PartialName()};
Junxiao Shi18739c42016-12-22 08:03:00 +0000130}
131
132Name
133Strategy::makeInstanceName(const Name& input, const Name& strategyName)
134{
135 BOOST_ASSERT(strategyName.at(-1).isVersion());
136
137 bool hasVersion = std::any_of(input.rbegin(), input.rend(),
138 [] (const name::Component& comp) { return comp.isVersion(); });
139 return hasVersion ? input : Name(input).append(strategyName.at(-1));
140}
141
142Strategy::Strategy(Forwarder& forwarder)
Junxiao Shicde37ad2015-12-24 01:02:05 -0700143 : afterAddFace(forwarder.getFaceTable().afterAdd)
144 , beforeRemoveFace(forwarder.getFaceTable().beforeRemove)
Junxiao Shibb5105f2014-03-03 12:06:45 -0700145 , m_forwarder(forwarder)
Junxiao Shi15e98b02016-08-12 11:21:44 +0000146 , m_measurements(m_forwarder.getMeasurements(), m_forwarder.getStrategyChoice(), *this)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700147{
148}
149
Junxiao Shi15e98b02016-08-12 11:21:44 +0000150Strategy::~Strategy() = default;
Junxiao Shid3c792f2014-01-30 00:46:13 -0700151
Junxiao Shi679e9272014-02-15 20:10:21 -0700152void
Junxiao Shi15e98b02016-08-12 11:21:44 +0000153Strategy::beforeSatisfyInterest(const shared_ptr<pit::Entry>& pitEntry,
Junxiao Shi82e7f582014-09-07 15:15:40 -0700154 const Face& inFace, const Data& data)
Junxiao Shi22be22c2014-02-16 22:53:48 -0700155{
Junxiao Shi82e7f582014-09-07 15:15:40 -0700156 NFD_LOG_DEBUG("beforeSatisfyInterest pitEntry=" << pitEntry->getName() <<
Junxiao Shi15e98b02016-08-12 11:21:44 +0000157 " inFace=" << inFace.getId() << " data=" << data.getName());
Junxiao Shi22be22c2014-02-16 22:53:48 -0700158}
159
160void
Teng Liang85a36632018-03-21 05:59:34 -0700161Strategy::afterContentStoreHit(const shared_ptr<pit::Entry>& pitEntry,
162 const Face& inFace, const Data& data)
163{
164 NFD_LOG_DEBUG("afterContentStoreHit pitEntry=" << pitEntry->getName() <<
165 " inFace=" << inFace.getId() << " data=" << data.getName());
166
167 this->sendData(pitEntry, data, inFace);
168}
169
170void
Teng Liang43bb2312018-03-26 04:16:42 -0700171Strategy::afterReceiveData(const shared_ptr<pit::Entry>& pitEntry,
172 const Face& inFace, const Data& data)
173{
174 NFD_LOG_DEBUG("afterReceiveData pitEntry=" << pitEntry->getName() <<
175 " inFace=" << inFace.getId() << " data=" << data.getName());
176
177 this->beforeSatisfyInterest(pitEntry, inFace, data);
178
179 this->sendDataToAll(pitEntry, inFace, data);
180}
181
182void
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700183Strategy::afterReceiveNack(const Face& inFace, const lp::Nack& nack,
Junxiao Shi15e98b02016-08-12 11:21:44 +0000184 const shared_ptr<pit::Entry>& pitEntry)
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700185{
186 NFD_LOG_DEBUG("afterReceiveNack inFace=" << inFace.getId() <<
187 " pitEntry=" << pitEntry->getName());
188}
189
190void
Eric Newberry41aba102017-11-01 16:42:13 -0700191Strategy::onDroppedInterest(const Face& outFace, const Interest& interest)
192{
193 NFD_LOG_DEBUG("onDroppedInterest outFace=" << outFace.getId() << " name=" << interest.getName());
194}
195
196void
Teng Liang43bb2312018-03-26 04:16:42 -0700197Strategy::sendData(const shared_ptr<pit::Entry>& pitEntry, const Data& data, const Face& outFace)
198{
199 BOOST_ASSERT(pitEntry->getInterest().matchesData(data));
200
201 // delete the PIT entry's in-record based on outFace,
202 // since Data is sent to outFace from which the Interest was received
203 pitEntry->deleteInRecord(outFace);
204
205 m_forwarder.onOutgoingData(data, *const_pointer_cast<Face>(outFace.shared_from_this()));
206}
207
208void
209Strategy::sendDataToAll(const shared_ptr<pit::Entry>& pitEntry, const Face& inFace, const Data& data)
210{
211 std::set<Face*> pendingDownstreams;
212 auto now = time::steady_clock::now();
213
214 // remember pending downstreams
215 for (const pit::InRecord& inRecord : pitEntry->getInRecords()) {
216 if (inRecord.getExpiry() > now) {
217 if (inRecord.getFace().getId() == inFace.getId() &&
218 inRecord.getFace().getLinkType() != ndn::nfd::LINK_TYPE_AD_HOC) {
219 continue;
220 }
221 pendingDownstreams.insert(&inRecord.getFace());
222 }
223 }
224
225 for (const Face* pendingDownstream : pendingDownstreams) {
226 this->sendData(pitEntry, data, *pendingDownstream);
227 }
228}
229
230void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000231Strategy::sendNacks(const shared_ptr<pit::Entry>& pitEntry, const lp::NackHeader& header,
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700232 std::initializer_list<const Face*> exceptFaces)
233{
234 // populate downstreams with all downstreams faces
235 std::unordered_set<const Face*> downstreams;
Junxiao Shi4846f372016-04-05 13:39:30 -0700236 std::transform(pitEntry->in_begin(), pitEntry->in_end(), std::inserter(downstreams, downstreams.end()),
Junxiao Shi9cff7792016-08-01 21:45:11 +0000237 [] (const pit::InRecord& inR) { return &inR.getFace(); });
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700238
239 // delete excluded faces
Junxiao Shi9cff7792016-08-01 21:45:11 +0000240 // .erase in a loop is more efficient than std::set_difference because that requires sorted range
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700241 for (const Face* exceptFace : exceptFaces) {
242 downstreams.erase(exceptFace);
243 }
244
245 // send Nacks
246 for (const Face* downstream : downstreams) {
247 this->sendNack(pitEntry, *downstream, header);
248 }
Junxiao Shi4846f372016-04-05 13:39:30 -0700249 // warning: don't loop on pitEntry->getInRecords(), because in-record is deleted when sending Nack
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700250}
Junxiao Shid3c792f2014-01-30 00:46:13 -0700251
Junxiao Shicf0f3ce2016-09-02 13:01:59 +0000252const fib::Entry&
253Strategy::lookupFib(const pit::Entry& pitEntry) const
254{
255 const Fib& fib = m_forwarder.getFib();
Junxiao Shicf0f3ce2016-09-02 13:01:59 +0000256
257 const Interest& interest = pitEntry.getInterest();
Junxiao Shifc2e13d2017-07-25 02:08:48 +0000258 // has forwarding hint?
259 if (interest.getForwardingHint().empty()) {
Junxiao Shicf0f3ce2016-09-02 13:01:59 +0000260 // FIB lookup with Interest name
261 const fib::Entry& fibEntry = fib.findLongestPrefixMatch(pitEntry);
Junxiao Shifc2e13d2017-07-25 02:08:48 +0000262 NFD_LOG_TRACE("lookupFib noForwardingHint found=" << fibEntry.getPrefix());
Junxiao Shicf0f3ce2016-09-02 13:01:59 +0000263 return fibEntry;
264 }
265
Junxiao Shifc2e13d2017-07-25 02:08:48 +0000266 const DelegationList& fh = interest.getForwardingHint();
267 // Forwarding hint should have been stripped by incoming Interest pipeline when reaching producer region
268 BOOST_ASSERT(!m_forwarder.getNetworkRegionTable().isInProducerRegion(fh));
Junxiao Shicf0f3ce2016-09-02 13:01:59 +0000269
Junxiao Shifc2e13d2017-07-25 02:08:48 +0000270 const fib::Entry* fibEntry = nullptr;
271 for (const Delegation& del : fh) {
272 fibEntry = &fib.findLongestPrefixMatch(del.name);
273 if (fibEntry->hasNextHops()) {
274 if (fibEntry->getPrefix().size() == 0) {
275 // in consumer region, return the default route
276 NFD_LOG_TRACE("lookupFib inConsumerRegion found=" << fibEntry->getPrefix());
277 }
278 else {
279 // in default-free zone, use the first delegation that finds a FIB entry
280 NFD_LOG_TRACE("lookupFib delegation=" << del.name << " found=" << fibEntry->getPrefix());
281 }
282 return *fibEntry;
Junxiao Shicf0f3ce2016-09-02 13:01:59 +0000283 }
Junxiao Shifc2e13d2017-07-25 02:08:48 +0000284 BOOST_ASSERT(fibEntry->getPrefix().size() == 0); // only ndn:/ FIB entry can have zero nexthop
Junxiao Shicf0f3ce2016-09-02 13:01:59 +0000285 }
Junxiao Shifc2e13d2017-07-25 02:08:48 +0000286 BOOST_ASSERT(fibEntry != nullptr && fibEntry->getPrefix().size() == 0);
287 return *fibEntry; // only occurs if no delegation finds a FIB nexthop
Junxiao Shicf0f3ce2016-09-02 13:01:59 +0000288}
289
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700290} // namespace fw
Junxiao Shid3c792f2014-01-30 00:46:13 -0700291} // namespace nfd