blob: 3dc9abd485b51f2bc8629b2aea0c83f4a784c0f9 [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 Liangebc20f62020-06-23 16:55:20 -07003 * Copyright (c) 2014-2020, 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 Shi606d5dd2019-09-23 12:47:44 -060030#include <ndn-cxx/lp/pit-token.hpp>
31
Junxiao Shic34d1672016-12-09 15:57:59 +000032#include <boost/range/adaptor/map.hpp>
33#include <boost/range/algorithm/copy.hpp>
Junxiao Shid3c792f2014-01-30 00:46:13 -070034
35namespace nfd {
Junxiao Shi8c8d2182014-01-30 22:33:00 -070036namespace fw {
Junxiao Shid3c792f2014-01-30 00:46:13 -070037
Davide Pesaventoa3148082018-04-12 18:21:54 -040038NFD_LOG_INIT(Strategy);
Junxiao Shi679e9272014-02-15 20:10:21 -070039
Junxiao Shic34d1672016-12-09 15:57:59 +000040Strategy::Registry&
41Strategy::getRegistry()
42{
43 static Registry registry;
44 return registry;
45}
46
47Strategy::Registry::const_iterator
Junxiao Shi18739c42016-12-22 08:03:00 +000048Strategy::find(const Name& instanceName)
Junxiao Shic34d1672016-12-09 15:57:59 +000049{
50 const Registry& registry = getRegistry();
Junxiao Shi18739c42016-12-22 08:03:00 +000051 ParsedInstanceName parsed = parseInstanceName(instanceName);
52
Junxiao Shi18739c42016-12-22 08:03:00 +000053 if (parsed.version) {
Junxiao Shi91f6ee02016-12-29 21:44:44 +000054 // 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();
Junxiao Shi18739c42016-12-22 08:03:00 +000066 }
67
Junxiao Shi91f6ee02016-12-29 21:44:44 +000068 // 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 }
Junxiao Shic34d1672016-12-09 15:57:59 +000078 }
79 }
Junxiao Shi91f6ee02016-12-29 21:44:44 +000080
81 NFD_LOG_TRACE("find " << instanceName << " unversioned not-found");
82 return registry.end();
Junxiao Shic34d1672016-12-09 15:57:59 +000083}
84
85bool
Junxiao Shi18739c42016-12-22 08:03:00 +000086Strategy::canCreate(const Name& instanceName)
Junxiao Shic34d1672016-12-09 15:57:59 +000087{
Junxiao Shi18739c42016-12-22 08:03:00 +000088 return Strategy::find(instanceName) != getRegistry().end();
Junxiao Shic34d1672016-12-09 15:57:59 +000089}
90
91unique_ptr<Strategy>
Junxiao Shi18739c42016-12-22 08:03:00 +000092Strategy::create(const Name& instanceName, Forwarder& forwarder)
Junxiao Shic34d1672016-12-09 15:57:59 +000093{
Junxiao Shi18739c42016-12-22 08:03:00 +000094 auto found = Strategy::find(instanceName);
Junxiao Shic34d1672016-12-09 15:57:59 +000095 if (found == getRegistry().end()) {
Junxiao Shi18739c42016-12-22 08:03:00 +000096 NFD_LOG_DEBUG("create " << instanceName << " not-found");
Junxiao Shic34d1672016-12-09 15:57:59 +000097 return nullptr;
98 }
99
Junxiao Shi18739c42016-12-22 08:03:00 +0000100 unique_ptr<Strategy> instance = found->second(forwarder, instanceName);
ashiqopuc7079482019-02-20 05:34:37 +0000101 NFD_LOG_DEBUG("create " << instanceName << " found=" << found->first
102 << " created=" << instance->getInstanceName());
Junxiao Shi18739c42016-12-22 08:03:00 +0000103 BOOST_ASSERT(!instance->getInstanceName().empty());
104 return instance;
Junxiao Shic34d1672016-12-09 15:57:59 +0000105}
106
Junxiao Shi55e21b92017-01-23 03:27:47 +0000107bool
108Strategy::areSameType(const Name& instanceNameA, const Name& instanceNameB)
109{
110 return Strategy::find(instanceNameA) == Strategy::find(instanceNameB);
111}
112
Junxiao Shic34d1672016-12-09 15:57:59 +0000113std::set<Name>
114Strategy::listRegistered()
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
Junxiao Shi18739c42016-12-22 08:03:00 +0000122Strategy::ParsedInstanceName
123Strategy::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 }
Davide Pesavento87fc0f82018-04-11 23:43:51 -0400130 return {input, nullopt, PartialName()};
Junxiao Shi18739c42016-12-22 08:03:00 +0000131}
132
133Name
134Strategy::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
143Strategy::Strategy(Forwarder& forwarder)
Davide Pesaventoa4abfb02019-10-06 16:02:56 -0400144 : afterAddFace(forwarder.m_faceTable.afterAdd)
145 , beforeRemoveFace(forwarder.m_faceTable.beforeRemove)
Junxiao Shibb5105f2014-03-03 12:06:45 -0700146 , m_forwarder(forwarder)
Junxiao Shi15e98b02016-08-12 11:21:44 +0000147 , m_measurements(m_forwarder.getMeasurements(), m_forwarder.getStrategyChoice(), *this)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700148{
149}
150
Junxiao Shi15e98b02016-08-12 11:21:44 +0000151Strategy::~Strategy() = default;
Junxiao Shid3c792f2014-01-30 00:46:13 -0700152
Junxiao Shi679e9272014-02-15 20:10:21 -0700153void
Junxiao Shi15e98b02016-08-12 11:21:44 +0000154Strategy::beforeSatisfyInterest(const shared_ptr<pit::Entry>& pitEntry,
ashiqopuc7079482019-02-20 05:34:37 +0000155 const FaceEndpoint& ingress, const Data& data)
Junxiao Shi22be22c2014-02-16 22:53:48 -0700156{
ashiqopuc7079482019-02-20 05:34:37 +0000157 NFD_LOG_DEBUG("beforeSatisfyInterest pitEntry=" << pitEntry->getName()
158 << " in=" << ingress << " data=" << data.getName());
Junxiao Shi22be22c2014-02-16 22:53:48 -0700159}
160
161void
Teng Liang85a36632018-03-21 05:59:34 -0700162Strategy::afterContentStoreHit(const shared_ptr<pit::Entry>& pitEntry,
ashiqopuc7079482019-02-20 05:34:37 +0000163 const FaceEndpoint& ingress, const Data& data)
Teng Liang85a36632018-03-21 05:59:34 -0700164{
ashiqopuc7079482019-02-20 05:34:37 +0000165 NFD_LOG_DEBUG("afterContentStoreHit pitEntry=" << pitEntry->getName()
166 << " in=" << ingress << " data=" << data.getName());
Teng Liang85a36632018-03-21 05:59:34 -0700167
Teng Liangebc20f62020-06-23 16:55:20 -0700168 this->sendData(pitEntry, data, ingress.face);
Teng Liang85a36632018-03-21 05:59:34 -0700169}
170
171void
Teng Liang43bb2312018-03-26 04:16:42 -0700172Strategy::afterReceiveData(const shared_ptr<pit::Entry>& pitEntry,
ashiqopuc7079482019-02-20 05:34:37 +0000173 const FaceEndpoint& ingress, const Data& data)
Teng Liang43bb2312018-03-26 04:16:42 -0700174{
ashiqopuc7079482019-02-20 05:34:37 +0000175 NFD_LOG_DEBUG("afterReceiveData pitEntry=" << pitEntry->getName()
176 << " in=" << ingress << " data=" << data.getName());
Teng Liang43bb2312018-03-26 04:16:42 -0700177
ashiqopuc7079482019-02-20 05:34:37 +0000178 this->beforeSatisfyInterest(pitEntry, ingress, data);
Teng Liang43bb2312018-03-26 04:16:42 -0700179
Teng Liangebc20f62020-06-23 16:55:20 -0700180 this->sendDataToAll(pitEntry, ingress.face, data);
Teng Liang43bb2312018-03-26 04:16:42 -0700181}
182
183void
Teng Liangebc20f62020-06-23 16:55:20 -0700184Strategy::afterReceiveNack(const FaceEndpoint& ingress, const lp::Nack&,
Junxiao Shi15e98b02016-08-12 11:21:44 +0000185 const shared_ptr<pit::Entry>& pitEntry)
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700186{
ashiqopuc7079482019-02-20 05:34:37 +0000187 NFD_LOG_DEBUG("afterReceiveNack in=" << ingress << " pitEntry=" << pitEntry->getName());
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700188}
189
190void
Teng Liangebc20f62020-06-23 16:55:20 -0700191Strategy::onDroppedInterest(const Face& egress, const Interest& interest)
Eric Newberry41aba102017-11-01 16:42:13 -0700192{
Teng Liangebc20f62020-06-23 16:55:20 -0700193 NFD_LOG_DEBUG("onDroppedInterest out=" << egress.getId() << " name=" << interest.getName());
Eric Newberry41aba102017-11-01 16:42:13 -0700194}
195
Eric Newberry2377ada2020-09-28 22:40:14 -0700196pit::OutRecord*
Teng Liangebc20f62020-06-23 16:55:20 -0700197Strategy::sendInterest(const shared_ptr<pit::Entry>& pitEntry, Face& egress, const Interest& interest)
Junxiao Shi606d5dd2019-09-23 12:47:44 -0600198{
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>();
Eric Newberry2377ada2020-09-28 22:40:14 -0700202 return m_forwarder.onOutgoingInterest(pitEntry, egress, interest2);
Junxiao Shi606d5dd2019-09-23 12:47:44 -0600203 }
Eric Newberry2377ada2020-09-28 22:40:14 -0700204 return m_forwarder.onOutgoingInterest(pitEntry, egress, interest);
Junxiao Shi606d5dd2019-09-23 12:47:44 -0600205}
206
207void
Ju Pan2feb4592019-09-16 20:56:38 +0000208Strategy::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
Eric Newberry2377ada2020-09-28 22:40:14 -0700214bool
Teng Liangebc20f62020-06-23 16:55:20 -0700215Strategy::sendData(const shared_ptr<pit::Entry>& pitEntry, const Data& data, Face& egress)
Teng Liang43bb2312018-03-26 04:16:42 -0700216{
217 BOOST_ASSERT(pitEntry->getInterest().matchesData(data));
218
Junxiao Shi606d5dd2019-09-23 12:47:44 -0600219 shared_ptr<lp::PitToken> pitToken;
Teng Liangebc20f62020-06-23 16:55:20 -0700220 auto inRecord = pitEntry->getInRecord(egress);
Junxiao Shi606d5dd2019-09-23 12:47:44 -0600221 if (inRecord != pitEntry->in_end()) {
222 pitToken = inRecord->getInterest().getTag<lp::PitToken>();
223 }
224
ashiqopuc7079482019-02-20 05:34:37 +0000225 // delete the PIT entry's in-record based on egress,
Teng Liangebc20f62020-06-23 16:55:20 -0700226 // since the Data is sent to the face from which the Interest was received
227 pitEntry->deleteInRecord(egress);
Teng Liang43bb2312018-03-26 04:16:42 -0700228
Junxiao Shi606d5dd2019-09-23 12:47:44 -0600229 if (pitToken != nullptr) {
230 Data data2 = data; // make a copy so each downstream can get a different PIT token
231 data2.setTag(pitToken);
Eric Newberry2377ada2020-09-28 22:40:14 -0700232 return m_forwarder.onOutgoingData(data2, egress);
Junxiao Shi606d5dd2019-09-23 12:47:44 -0600233 }
Eric Newberry2377ada2020-09-28 22:40:14 -0700234 return m_forwarder.onOutgoingData(data, egress);
Teng Liang43bb2312018-03-26 04:16:42 -0700235}
236
237void
ashiqopuc7079482019-02-20 05:34:37 +0000238Strategy::sendDataToAll(const shared_ptr<pit::Entry>& pitEntry,
Teng Liangebc20f62020-06-23 16:55:20 -0700239 const Face& inFace, const Data& data)
Teng Liang43bb2312018-03-26 04:16:42 -0700240{
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000241 std::set<Face*> pendingDownstreams;
Teng Liang43bb2312018-03-26 04:16:42 -0700242 auto now = time::steady_clock::now();
243
244 // remember pending downstreams
245 for (const pit::InRecord& inRecord : pitEntry->getInRecords()) {
246 if (inRecord.getExpiry() > now) {
Teng Liangebc20f62020-06-23 16:55:20 -0700247 if (inRecord.getFace().getId() == inFace.getId() &&
Teng Liang43bb2312018-03-26 04:16:42 -0700248 inRecord.getFace().getLinkType() != ndn::nfd::LINK_TYPE_AD_HOC) {
249 continue;
250 }
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000251 pendingDownstreams.emplace(&inRecord.getFace());
Teng Liang43bb2312018-03-26 04:16:42 -0700252 }
253 }
254
ashiqopuc7079482019-02-20 05:34:37 +0000255 for (const auto& pendingDownstream : pendingDownstreams) {
Teng Liangebc20f62020-06-23 16:55:20 -0700256 this->sendData(pitEntry, data, *pendingDownstream);
Teng Liang43bb2312018-03-26 04:16:42 -0700257 }
258}
259
260void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000261Strategy::sendNacks(const shared_ptr<pit::Entry>& pitEntry, const lp::NackHeader& header,
Teng Liangebc20f62020-06-23 16:55:20 -0700262 std::initializer_list<const Face*> exceptFaces)
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700263{
264 // populate downstreams with all downstreams faces
Teng Liangebc20f62020-06-23 16:55:20 -0700265 std::unordered_set<Face*> downstreams;
Junxiao Shi4846f372016-04-05 13:39:30 -0700266 std::transform(pitEntry->in_begin(), pitEntry->in_end(), std::inserter(downstreams, downstreams.end()),
Teng Liangebc20f62020-06-23 16:55:20 -0700267 [] (const pit::InRecord& inR) { return &inR.getFace(); });
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700268
269 // delete excluded faces
Teng Liangebc20f62020-06-23 16:55:20 -0700270 for (auto exceptFace : exceptFaces) {
271 downstreams.erase(const_cast<Face*>(exceptFace));
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700272 }
273
274 // send Nacks
Teng Liangebc20f62020-06-23 16:55:20 -0700275 for (auto downstream : downstreams) {
276 this->sendNack(pitEntry, *downstream, header);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700277 }
Junxiao Shi4846f372016-04-05 13:39:30 -0700278 // warning: don't loop on pitEntry->getInRecords(), because in-record is deleted when sending Nack
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700279}
Junxiao Shid3c792f2014-01-30 00:46:13 -0700280
Junxiao Shicf0f3ce2016-09-02 13:01:59 +0000281const fib::Entry&
282Strategy::lookupFib(const pit::Entry& pitEntry) const
283{
284 const Fib& fib = m_forwarder.getFib();
Junxiao Shicf0f3ce2016-09-02 13:01:59 +0000285
286 const Interest& interest = pitEntry.getInterest();
Junxiao Shifc2e13d2017-07-25 02:08:48 +0000287 // has forwarding hint?
288 if (interest.getForwardingHint().empty()) {
Junxiao Shicf0f3ce2016-09-02 13:01:59 +0000289 // FIB lookup with Interest name
290 const fib::Entry& fibEntry = fib.findLongestPrefixMatch(pitEntry);
Junxiao Shifc2e13d2017-07-25 02:08:48 +0000291 NFD_LOG_TRACE("lookupFib noForwardingHint found=" << fibEntry.getPrefix());
Junxiao Shicf0f3ce2016-09-02 13:01:59 +0000292 return fibEntry;
293 }
294
Junxiao Shifc2e13d2017-07-25 02:08:48 +0000295 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));
Junxiao Shicf0f3ce2016-09-02 13:01:59 +0000298
Junxiao Shifc2e13d2017-07-25 02:08:48 +0000299 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;
Junxiao Shicf0f3ce2016-09-02 13:01:59 +0000312 }
Junxiao Shifc2e13d2017-07-25 02:08:48 +0000313 BOOST_ASSERT(fibEntry->getPrefix().size() == 0); // only ndn:/ FIB entry can have zero nexthop
Junxiao Shicf0f3ce2016-09-02 13:01:59 +0000314 }
Junxiao Shifc2e13d2017-07-25 02:08:48 +0000315 BOOST_ASSERT(fibEntry != nullptr && fibEntry->getPrefix().size() == 0);
316 return *fibEntry; // only occurs if no delegation finds a FIB nexthop
Junxiao Shicf0f3ce2016-09-02 13:01:59 +0000317}
318
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700319} // namespace fw
Junxiao Shid3c792f2014-01-30 00:46:13 -0700320} // namespace nfd