blob: a5519faf6304db7dc6f38d55a9d980dfa566b6e0 [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/*
Davide Pesaventob8bd5ee2019-02-03 22:53:40 -05003 * Copyright (c) 2014-2019, 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"
Davide Pesavento2cae8ca2019-04-18 20:48:05 -040028#include "common/logger.hpp"
Davide Pesaventoa3148082018-04-12 18:21:54 -040029
Junxiao Shic34d1672016-12-09 15:57:59 +000030#include <boost/range/adaptor/map.hpp>
31#include <boost/range/algorithm/copy.hpp>
Junxiao Shid3c792f2014-01-30 00:46:13 -070032
33namespace nfd {
Junxiao Shi8c8d2182014-01-30 22:33:00 -070034namespace fw {
Junxiao Shid3c792f2014-01-30 00:46:13 -070035
Davide Pesaventoa3148082018-04-12 18:21:54 -040036NFD_LOG_INIT(Strategy);
Junxiao Shi679e9272014-02-15 20:10:21 -070037
Junxiao Shic34d1672016-12-09 15:57:59 +000038Strategy::Registry&
39Strategy::getRegistry()
40{
41 static Registry registry;
42 return registry;
43}
44
45Strategy::Registry::const_iterator
Junxiao Shi18739c42016-12-22 08:03:00 +000046Strategy::find(const Name& instanceName)
Junxiao Shic34d1672016-12-09 15:57:59 +000047{
48 const Registry& registry = getRegistry();
Junxiao Shi18739c42016-12-22 08:03:00 +000049 ParsedInstanceName parsed = parseInstanceName(instanceName);
50
Junxiao Shi18739c42016-12-22 08:03:00 +000051 if (parsed.version) {
Junxiao Shi91f6ee02016-12-29 21:44:44 +000052 // specified version: find exact or next higher version
53
54 auto found = registry.lower_bound(parsed.strategyName);
55 if (found != registry.end()) {
56 if (parsed.strategyName.getPrefix(-1).isPrefixOf(found->first)) {
57 NFD_LOG_TRACE("find " << instanceName << " versioned found=" << found->first);
58 return found;
59 }
60 }
61
62 NFD_LOG_TRACE("find " << instanceName << " versioned not-found");
63 return registry.end();
Junxiao Shi18739c42016-12-22 08:03:00 +000064 }
65
Junxiao Shi91f6ee02016-12-29 21:44:44 +000066 // no version specified: find highest version
67
68 if (!parsed.strategyName.empty()) { // Name().getSuccessor() would be invalid
69 auto found = registry.lower_bound(parsed.strategyName.getSuccessor());
70 if (found != registry.begin()) {
71 --found;
72 if (parsed.strategyName.isPrefixOf(found->first)) {
73 NFD_LOG_TRACE("find " << instanceName << " unversioned found=" << found->first);
74 return found;
75 }
Junxiao Shic34d1672016-12-09 15:57:59 +000076 }
77 }
Junxiao Shi91f6ee02016-12-29 21:44:44 +000078
79 NFD_LOG_TRACE("find " << instanceName << " unversioned not-found");
80 return registry.end();
Junxiao Shic34d1672016-12-09 15:57:59 +000081}
82
83bool
Junxiao Shi18739c42016-12-22 08:03:00 +000084Strategy::canCreate(const Name& instanceName)
Junxiao Shic34d1672016-12-09 15:57:59 +000085{
Junxiao Shi18739c42016-12-22 08:03:00 +000086 return Strategy::find(instanceName) != getRegistry().end();
Junxiao Shic34d1672016-12-09 15:57:59 +000087}
88
89unique_ptr<Strategy>
Junxiao Shi18739c42016-12-22 08:03:00 +000090Strategy::create(const Name& instanceName, Forwarder& forwarder)
Junxiao Shic34d1672016-12-09 15:57:59 +000091{
Junxiao Shi18739c42016-12-22 08:03:00 +000092 auto found = Strategy::find(instanceName);
Junxiao Shic34d1672016-12-09 15:57:59 +000093 if (found == getRegistry().end()) {
Junxiao Shi18739c42016-12-22 08:03:00 +000094 NFD_LOG_DEBUG("create " << instanceName << " not-found");
Junxiao Shic34d1672016-12-09 15:57:59 +000095 return nullptr;
96 }
97
Junxiao Shi18739c42016-12-22 08:03:00 +000098 unique_ptr<Strategy> instance = found->second(forwarder, instanceName);
ashiqopuc7079482019-02-20 05:34:37 +000099 NFD_LOG_DEBUG("create " << instanceName << " found=" << found->first
100 << " created=" << instance->getInstanceName());
Junxiao Shi18739c42016-12-22 08:03:00 +0000101 BOOST_ASSERT(!instance->getInstanceName().empty());
102 return instance;
Junxiao Shic34d1672016-12-09 15:57:59 +0000103}
104
Junxiao Shi55e21b92017-01-23 03:27:47 +0000105bool
106Strategy::areSameType(const Name& instanceNameA, const Name& instanceNameB)
107{
108 return Strategy::find(instanceNameA) == Strategy::find(instanceNameB);
109}
110
Junxiao Shic34d1672016-12-09 15:57:59 +0000111std::set<Name>
112Strategy::listRegistered()
113{
114 std::set<Name> strategyNames;
115 boost::copy(getRegistry() | boost::adaptors::map_keys,
116 std::inserter(strategyNames, strategyNames.end()));
117 return strategyNames;
118}
119
Junxiao Shi18739c42016-12-22 08:03:00 +0000120Strategy::ParsedInstanceName
121Strategy::parseInstanceName(const Name& input)
122{
123 for (ssize_t i = input.size() - 1; i > 0; --i) {
124 if (input[i].isVersion()) {
125 return {input.getPrefix(i + 1), input[i].toVersion(), input.getSubName(i + 1)};
126 }
127 }
Davide Pesavento87fc0f82018-04-11 23:43:51 -0400128 return {input, nullopt, PartialName()};
Junxiao Shi18739c42016-12-22 08:03:00 +0000129}
130
131Name
132Strategy::makeInstanceName(const Name& input, const Name& strategyName)
133{
134 BOOST_ASSERT(strategyName.at(-1).isVersion());
135
136 bool hasVersion = std::any_of(input.rbegin(), input.rend(),
137 [] (const name::Component& comp) { return comp.isVersion(); });
138 return hasVersion ? input : Name(input).append(strategyName.at(-1));
139}
140
141Strategy::Strategy(Forwarder& forwarder)
Junxiao Shicde37ad2015-12-24 01:02:05 -0700142 : afterAddFace(forwarder.getFaceTable().afterAdd)
143 , beforeRemoveFace(forwarder.getFaceTable().beforeRemove)
Junxiao Shibb5105f2014-03-03 12:06:45 -0700144 , m_forwarder(forwarder)
Junxiao Shi15e98b02016-08-12 11:21:44 +0000145 , m_measurements(m_forwarder.getMeasurements(), m_forwarder.getStrategyChoice(), *this)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700146{
147}
148
Junxiao Shi15e98b02016-08-12 11:21:44 +0000149Strategy::~Strategy() = default;
Junxiao Shid3c792f2014-01-30 00:46:13 -0700150
Alexander Afanasyevbdb6b862019-07-29 11:45:37 -0400151
152void
153Strategy::afterReceiveLoopedInterest(const FaceEndpoint& ingress, const Interest& interest,
154 pit::Entry& pitEntry)
155{
156 NFD_LOG_DEBUG("afterReceiveLoopedInterest pitEntry=" << pitEntry.getName()
157 << " in=" << ingress);
158}
159
Junxiao Shi679e9272014-02-15 20:10:21 -0700160void
Junxiao Shi15e98b02016-08-12 11:21:44 +0000161Strategy::beforeSatisfyInterest(const shared_ptr<pit::Entry>& pitEntry,
ashiqopuc7079482019-02-20 05:34:37 +0000162 const FaceEndpoint& ingress, const Data& data)
Junxiao Shi22be22c2014-02-16 22:53:48 -0700163{
ashiqopuc7079482019-02-20 05:34:37 +0000164 NFD_LOG_DEBUG("beforeSatisfyInterest pitEntry=" << pitEntry->getName()
165 << " in=" << ingress << " data=" << data.getName());
Junxiao Shi22be22c2014-02-16 22:53:48 -0700166}
167
168void
Teng Liang85a36632018-03-21 05:59:34 -0700169Strategy::afterContentStoreHit(const shared_ptr<pit::Entry>& pitEntry,
ashiqopuc7079482019-02-20 05:34:37 +0000170 const FaceEndpoint& ingress, const Data& data)
Teng Liang85a36632018-03-21 05:59:34 -0700171{
ashiqopuc7079482019-02-20 05:34:37 +0000172 NFD_LOG_DEBUG("afterContentStoreHit pitEntry=" << pitEntry->getName()
173 << " in=" << ingress << " data=" << data.getName());
Teng Liang85a36632018-03-21 05:59:34 -0700174
ashiqopuc7079482019-02-20 05:34:37 +0000175 this->sendData(pitEntry, data, ingress);
Teng Liang85a36632018-03-21 05:59:34 -0700176}
177
178void
Teng Liang43bb2312018-03-26 04:16:42 -0700179Strategy::afterReceiveData(const shared_ptr<pit::Entry>& pitEntry,
ashiqopuc7079482019-02-20 05:34:37 +0000180 const FaceEndpoint& ingress, const Data& data)
Teng Liang43bb2312018-03-26 04:16:42 -0700181{
ashiqopuc7079482019-02-20 05:34:37 +0000182 NFD_LOG_DEBUG("afterReceiveData pitEntry=" << pitEntry->getName()
183 << " in=" << ingress << " data=" << data.getName());
Teng Liang43bb2312018-03-26 04:16:42 -0700184
ashiqopuc7079482019-02-20 05:34:37 +0000185 this->beforeSatisfyInterest(pitEntry, ingress, data);
Teng Liang43bb2312018-03-26 04:16:42 -0700186
ashiqopuc7079482019-02-20 05:34:37 +0000187 this->sendDataToAll(pitEntry, ingress, data);
Teng Liang43bb2312018-03-26 04:16:42 -0700188}
189
190void
ashiqopuc7079482019-02-20 05:34:37 +0000191Strategy::afterReceiveNack(const FaceEndpoint& ingress, const lp::Nack& nack,
Junxiao Shi15e98b02016-08-12 11:21:44 +0000192 const shared_ptr<pit::Entry>& pitEntry)
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700193{
ashiqopuc7079482019-02-20 05:34:37 +0000194 NFD_LOG_DEBUG("afterReceiveNack in=" << ingress << " pitEntry=" << pitEntry->getName());
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700195}
196
197void
ashiqopuc7079482019-02-20 05:34:37 +0000198Strategy::onDroppedInterest(const FaceEndpoint& egress, const Interest& interest)
Eric Newberry41aba102017-11-01 16:42:13 -0700199{
ashiqopuc7079482019-02-20 05:34:37 +0000200 NFD_LOG_DEBUG("onDroppedInterest out=" << egress << " name=" << interest.getName());
Eric Newberry41aba102017-11-01 16:42:13 -0700201}
202
203void
ashiqopuc7079482019-02-20 05:34:37 +0000204Strategy::sendData(const shared_ptr<pit::Entry>& pitEntry, const Data& data,
205 const FaceEndpoint& egress)
Teng Liang43bb2312018-03-26 04:16:42 -0700206{
207 BOOST_ASSERT(pitEntry->getInterest().matchesData(data));
208
ashiqopuc7079482019-02-20 05:34:37 +0000209 // delete the PIT entry's in-record based on egress,
210 // since Data is sent to face and endpoint from which the Interest was received
211 pitEntry->deleteInRecord(egress.face, egress.endpoint);
Teng Liang43bb2312018-03-26 04:16:42 -0700212
ashiqopuc7079482019-02-20 05:34:37 +0000213 m_forwarder.onOutgoingData(data, egress);
Teng Liang43bb2312018-03-26 04:16:42 -0700214}
215
216void
ashiqopuc7079482019-02-20 05:34:37 +0000217Strategy::sendDataToAll(const shared_ptr<pit::Entry>& pitEntry,
218 const FaceEndpoint& ingress, const Data& data)
Teng Liang43bb2312018-03-26 04:16:42 -0700219{
ashiqopuc7079482019-02-20 05:34:37 +0000220 std::set<std::pair<Face*, EndpointId>> pendingDownstreams;
Teng Liang43bb2312018-03-26 04:16:42 -0700221 auto now = time::steady_clock::now();
222
223 // remember pending downstreams
224 for (const pit::InRecord& inRecord : pitEntry->getInRecords()) {
225 if (inRecord.getExpiry() > now) {
ashiqopuc7079482019-02-20 05:34:37 +0000226 if (inRecord.getFace().getId() == ingress.face.getId() &&
227 inRecord.getEndpointId() == ingress.endpoint &&
Teng Liang43bb2312018-03-26 04:16:42 -0700228 inRecord.getFace().getLinkType() != ndn::nfd::LINK_TYPE_AD_HOC) {
229 continue;
230 }
ashiqopuc7079482019-02-20 05:34:37 +0000231 pendingDownstreams.emplace(&inRecord.getFace(), inRecord.getEndpointId());
Teng Liang43bb2312018-03-26 04:16:42 -0700232 }
233 }
234
ashiqopuc7079482019-02-20 05:34:37 +0000235 for (const auto& pendingDownstream : pendingDownstreams) {
236 this->sendData(pitEntry, data, FaceEndpoint(*pendingDownstream.first, pendingDownstream.second));
Teng Liang43bb2312018-03-26 04:16:42 -0700237 }
238}
239
240void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000241Strategy::sendNacks(const shared_ptr<pit::Entry>& pitEntry, const lp::NackHeader& header,
ashiqopuc7079482019-02-20 05:34:37 +0000242 std::initializer_list<FaceEndpoint> exceptFaceEndpoints)
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700243{
244 // populate downstreams with all downstreams faces
ashiqopuc7079482019-02-20 05:34:37 +0000245 std::set<std::pair<Face*, EndpointId>> downstreams;
Junxiao Shi4846f372016-04-05 13:39:30 -0700246 std::transform(pitEntry->in_begin(), pitEntry->in_end(), std::inserter(downstreams, downstreams.end()),
ashiqopuc7079482019-02-20 05:34:37 +0000247 [] (const pit::InRecord& inR) {
248 return std::make_pair(&inR.getFace(), inR.getEndpointId());
249 });
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700250
251 // delete excluded faces
ashiqopuc7079482019-02-20 05:34:37 +0000252 for (const auto& exceptFaceEndpoint : exceptFaceEndpoints) {
253 downstreams.erase({&exceptFaceEndpoint.face, exceptFaceEndpoint.endpoint});
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700254 }
255
256 // send Nacks
ashiqopuc7079482019-02-20 05:34:37 +0000257 for (const auto& downstream : downstreams) {
258 this->sendNack(pitEntry, FaceEndpoint(*downstream.first, downstream.second), header);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700259 }
Junxiao Shi4846f372016-04-05 13:39:30 -0700260 // warning: don't loop on pitEntry->getInRecords(), because in-record is deleted when sending Nack
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700261}
Junxiao Shid3c792f2014-01-30 00:46:13 -0700262
Junxiao Shicf0f3ce2016-09-02 13:01:59 +0000263const fib::Entry&
264Strategy::lookupFib(const pit::Entry& pitEntry) const
265{
266 const Fib& fib = m_forwarder.getFib();
Junxiao Shicf0f3ce2016-09-02 13:01:59 +0000267
268 const Interest& interest = pitEntry.getInterest();
Junxiao Shifc2e13d2017-07-25 02:08:48 +0000269 // has forwarding hint?
270 if (interest.getForwardingHint().empty()) {
Junxiao Shicf0f3ce2016-09-02 13:01:59 +0000271 // FIB lookup with Interest name
272 const fib::Entry& fibEntry = fib.findLongestPrefixMatch(pitEntry);
Junxiao Shifc2e13d2017-07-25 02:08:48 +0000273 NFD_LOG_TRACE("lookupFib noForwardingHint found=" << fibEntry.getPrefix());
Junxiao Shicf0f3ce2016-09-02 13:01:59 +0000274 return fibEntry;
275 }
276
Junxiao Shifc2e13d2017-07-25 02:08:48 +0000277 const DelegationList& fh = interest.getForwardingHint();
278 // Forwarding hint should have been stripped by incoming Interest pipeline when reaching producer region
279 BOOST_ASSERT(!m_forwarder.getNetworkRegionTable().isInProducerRegion(fh));
Junxiao Shicf0f3ce2016-09-02 13:01:59 +0000280
Junxiao Shifc2e13d2017-07-25 02:08:48 +0000281 const fib::Entry* fibEntry = nullptr;
282 for (const Delegation& del : fh) {
283 fibEntry = &fib.findLongestPrefixMatch(del.name);
284 if (fibEntry->hasNextHops()) {
285 if (fibEntry->getPrefix().size() == 0) {
286 // in consumer region, return the default route
287 NFD_LOG_TRACE("lookupFib inConsumerRegion found=" << fibEntry->getPrefix());
288 }
289 else {
290 // in default-free zone, use the first delegation that finds a FIB entry
291 NFD_LOG_TRACE("lookupFib delegation=" << del.name << " found=" << fibEntry->getPrefix());
292 }
293 return *fibEntry;
Junxiao Shicf0f3ce2016-09-02 13:01:59 +0000294 }
Junxiao Shifc2e13d2017-07-25 02:08:48 +0000295 BOOST_ASSERT(fibEntry->getPrefix().size() == 0); // only ndn:/ FIB entry can have zero nexthop
Junxiao Shicf0f3ce2016-09-02 13:01:59 +0000296 }
Junxiao Shifc2e13d2017-07-25 02:08:48 +0000297 BOOST_ASSERT(fibEntry != nullptr && fibEntry->getPrefix().size() == 0);
298 return *fibEntry; // only occurs if no delegation finds a FIB nexthop
Junxiao Shicf0f3ce2016-09-02 13:01:59 +0000299}
300
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700301} // namespace fw
Junxiao Shid3c792f2014-01-30 00:46:13 -0700302} // namespace nfd