blob: 1f7babc051ca1dd612281d73aac3e31cdd0e2cbb [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/*
Junxiao Shi727b6102022-01-11 18:40:04 +00003 * Copyright (c) 2014-2022, 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>
Davide Pesaventob7bfcb92022-05-22 23:55:23 -040034#include <unordered_set>
Junxiao Shid3c792f2014-01-30 00:46:13 -070035
36namespace nfd {
Junxiao Shi8c8d2182014-01-30 22:33:00 -070037namespace fw {
Junxiao Shid3c792f2014-01-30 00:46:13 -070038
Davide Pesaventoa3148082018-04-12 18:21:54 -040039NFD_LOG_INIT(Strategy);
Junxiao Shi679e9272014-02-15 20:10:21 -070040
Junxiao Shic34d1672016-12-09 15:57:59 +000041Strategy::Registry&
42Strategy::getRegistry()
43{
44 static Registry registry;
45 return registry;
46}
47
48Strategy::Registry::const_iterator
Junxiao Shi18739c42016-12-22 08:03:00 +000049Strategy::find(const Name& instanceName)
Junxiao Shic34d1672016-12-09 15:57:59 +000050{
51 const Registry& registry = getRegistry();
Junxiao Shi18739c42016-12-22 08:03:00 +000052 ParsedInstanceName parsed = parseInstanceName(instanceName);
53
Junxiao Shi18739c42016-12-22 08:03:00 +000054 if (parsed.version) {
Junxiao Shi91f6ee02016-12-29 21:44:44 +000055 // specified version: find exact or next higher version
56
57 auto found = registry.lower_bound(parsed.strategyName);
58 if (found != registry.end()) {
59 if (parsed.strategyName.getPrefix(-1).isPrefixOf(found->first)) {
60 NFD_LOG_TRACE("find " << instanceName << " versioned found=" << found->first);
61 return found;
62 }
63 }
64
65 NFD_LOG_TRACE("find " << instanceName << " versioned not-found");
66 return registry.end();
Junxiao Shi18739c42016-12-22 08:03:00 +000067 }
68
Junxiao Shi91f6ee02016-12-29 21:44:44 +000069 // no version specified: find highest version
70
71 if (!parsed.strategyName.empty()) { // Name().getSuccessor() would be invalid
72 auto found = registry.lower_bound(parsed.strategyName.getSuccessor());
73 if (found != registry.begin()) {
74 --found;
75 if (parsed.strategyName.isPrefixOf(found->first)) {
76 NFD_LOG_TRACE("find " << instanceName << " unversioned found=" << found->first);
77 return found;
78 }
Junxiao Shic34d1672016-12-09 15:57:59 +000079 }
80 }
Junxiao Shi91f6ee02016-12-29 21:44:44 +000081
82 NFD_LOG_TRACE("find " << instanceName << " unversioned not-found");
83 return registry.end();
Junxiao Shic34d1672016-12-09 15:57:59 +000084}
85
86bool
Junxiao Shi18739c42016-12-22 08:03:00 +000087Strategy::canCreate(const Name& instanceName)
Junxiao Shic34d1672016-12-09 15:57:59 +000088{
Junxiao Shi18739c42016-12-22 08:03:00 +000089 return Strategy::find(instanceName) != getRegistry().end();
Junxiao Shic34d1672016-12-09 15:57:59 +000090}
91
92unique_ptr<Strategy>
Junxiao Shi18739c42016-12-22 08:03:00 +000093Strategy::create(const Name& instanceName, Forwarder& forwarder)
Junxiao Shic34d1672016-12-09 15:57:59 +000094{
Junxiao Shi18739c42016-12-22 08:03:00 +000095 auto found = Strategy::find(instanceName);
Junxiao Shic34d1672016-12-09 15:57:59 +000096 if (found == getRegistry().end()) {
Junxiao Shi18739c42016-12-22 08:03:00 +000097 NFD_LOG_DEBUG("create " << instanceName << " not-found");
Junxiao Shic34d1672016-12-09 15:57:59 +000098 return nullptr;
99 }
100
Junxiao Shi18739c42016-12-22 08:03:00 +0000101 unique_ptr<Strategy> instance = found->second(forwarder, instanceName);
ashiqopuc7079482019-02-20 05:34:37 +0000102 NFD_LOG_DEBUG("create " << instanceName << " found=" << found->first
103 << " created=" << instance->getInstanceName());
Junxiao Shi18739c42016-12-22 08:03:00 +0000104 BOOST_ASSERT(!instance->getInstanceName().empty());
105 return instance;
Junxiao Shic34d1672016-12-09 15:57:59 +0000106}
107
Junxiao Shi55e21b92017-01-23 03:27:47 +0000108bool
109Strategy::areSameType(const Name& instanceNameA, const Name& instanceNameB)
110{
111 return Strategy::find(instanceNameA) == Strategy::find(instanceNameB);
112}
113
Junxiao Shic34d1672016-12-09 15:57:59 +0000114std::set<Name>
115Strategy::listRegistered()
116{
117 std::set<Name> strategyNames;
118 boost::copy(getRegistry() | boost::adaptors::map_keys,
119 std::inserter(strategyNames, strategyNames.end()));
120 return strategyNames;
121}
122
Junxiao Shi18739c42016-12-22 08:03:00 +0000123Strategy::ParsedInstanceName
124Strategy::parseInstanceName(const Name& input)
125{
126 for (ssize_t i = input.size() - 1; i > 0; --i) {
127 if (input[i].isVersion()) {
128 return {input.getPrefix(i + 1), input[i].toVersion(), input.getSubName(i + 1)};
129 }
130 }
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400131 return {input, std::nullopt, PartialName()};
Junxiao Shi18739c42016-12-22 08:03:00 +0000132}
133
134Name
135Strategy::makeInstanceName(const Name& input, const Name& strategyName)
136{
137 BOOST_ASSERT(strategyName.at(-1).isVersion());
138
139 bool hasVersion = std::any_of(input.rbegin(), input.rend(),
Davide Pesavento0498ce82021-06-14 02:02:21 -0400140 [] (const auto& comp) { return comp.isVersion(); });
Junxiao Shi18739c42016-12-22 08:03:00 +0000141 return hasVersion ? input : Name(input).append(strategyName.at(-1));
142}
143
Ashlesh Gawande1ef93d02022-04-08 00:25:06 -0400144StrategyParameters
145Strategy::parseParameters(const PartialName& params)
146{
147 StrategyParameters parsed;
148
149 for (const auto& component : params) {
150 auto sep = std::find(component.value_begin(), component.value_end(), '~');
151 if (sep == component.value_end()) {
152 NDN_THROW(std::invalid_argument("Strategy parameters format is (<parameter>~<value>)*"));
153 }
154
155 std::string p(component.value_begin(), sep);
156 std::advance(sep, 1);
157 std::string v(sep, component.value_end());
158 if (p.empty() || v.empty()) {
159 NDN_THROW(std::invalid_argument("Strategy parameter name and value cannot be empty"));
160 }
161 parsed[std::move(p)] = std::move(v);
162 }
163
164 return parsed;
165}
166
Junxiao Shi18739c42016-12-22 08:03:00 +0000167Strategy::Strategy(Forwarder& forwarder)
Davide Pesaventoa4abfb02019-10-06 16:02:56 -0400168 : afterAddFace(forwarder.m_faceTable.afterAdd)
169 , beforeRemoveFace(forwarder.m_faceTable.beforeRemove)
Junxiao Shibb5105f2014-03-03 12:06:45 -0700170 , m_forwarder(forwarder)
Junxiao Shi15e98b02016-08-12 11:21:44 +0000171 , m_measurements(m_forwarder.getMeasurements(), m_forwarder.getStrategyChoice(), *this)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700172{
173}
174
Junxiao Shi15e98b02016-08-12 11:21:44 +0000175Strategy::~Strategy() = default;
Junxiao Shid3c792f2014-01-30 00:46:13 -0700176
Junxiao Shi679e9272014-02-15 20:10:21 -0700177void
Davide Pesavento0498ce82021-06-14 02:02:21 -0400178Strategy::afterContentStoreHit(const Data& data, const FaceEndpoint& ingress,
179 const shared_ptr<pit::Entry>& pitEntry)
180{
181 NFD_LOG_DEBUG("afterContentStoreHit pitEntry=" << pitEntry->getName()
182 << " in=" << ingress << " data=" << data.getName());
183
184 this->sendData(data, ingress.face, pitEntry);
185}
186
187void
188Strategy::beforeSatisfyInterest(const Data& data, const FaceEndpoint& ingress,
189 const shared_ptr<pit::Entry>& pitEntry)
Junxiao Shi22be22c2014-02-16 22:53:48 -0700190{
ashiqopuc7079482019-02-20 05:34:37 +0000191 NFD_LOG_DEBUG("beforeSatisfyInterest pitEntry=" << pitEntry->getName()
192 << " in=" << ingress << " data=" << data.getName());
Junxiao Shi22be22c2014-02-16 22:53:48 -0700193}
194
195void
Davide Pesavento0498ce82021-06-14 02:02:21 -0400196Strategy::afterReceiveData(const Data& data, const FaceEndpoint& ingress,
197 const shared_ptr<pit::Entry>& pitEntry)
Teng Liang43bb2312018-03-26 04:16:42 -0700198{
ashiqopuc7079482019-02-20 05:34:37 +0000199 NFD_LOG_DEBUG("afterReceiveData pitEntry=" << pitEntry->getName()
200 << " in=" << ingress << " data=" << data.getName());
Teng Liang43bb2312018-03-26 04:16:42 -0700201
Davide Pesavento0498ce82021-06-14 02:02:21 -0400202 this->beforeSatisfyInterest(data, ingress, pitEntry);
203 this->sendDataToAll(data, pitEntry, ingress.face);
Teng Liang43bb2312018-03-26 04:16:42 -0700204}
205
206void
Davide Pesavento0498ce82021-06-14 02:02:21 -0400207Strategy::afterReceiveNack(const lp::Nack&, const FaceEndpoint& ingress,
Junxiao Shi15e98b02016-08-12 11:21:44 +0000208 const shared_ptr<pit::Entry>& pitEntry)
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700209{
ashiqopuc7079482019-02-20 05:34:37 +0000210 NFD_LOG_DEBUG("afterReceiveNack in=" << ingress << " pitEntry=" << pitEntry->getName());
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700211}
212
213void
Davide Pesavento0498ce82021-06-14 02:02:21 -0400214Strategy::onDroppedInterest(const Interest& interest, Face& egress)
Eric Newberry41aba102017-11-01 16:42:13 -0700215{
Teng Liangebc20f62020-06-23 16:55:20 -0700216 NFD_LOG_DEBUG("onDroppedInterest out=" << egress.getId() << " name=" << interest.getName());
Eric Newberry41aba102017-11-01 16:42:13 -0700217}
218
Junxiao Shi606d5dd2019-09-23 12:47:44 -0600219void
Ju Pan2feb4592019-09-16 20:56:38 +0000220Strategy::afterNewNextHop(const fib::NextHop& nextHop, const shared_ptr<pit::Entry>& pitEntry)
221{
222 NFD_LOG_DEBUG("afterNewNextHop pitEntry=" << pitEntry->getName()
223 << " nexthop=" << nextHop.getFace().getId());
224}
225
Davide Pesavento0498ce82021-06-14 02:02:21 -0400226pit::OutRecord*
227Strategy::sendInterest(const Interest& interest, Face& egress, const shared_ptr<pit::Entry>& pitEntry)
228{
229 if (interest.getTag<lp::PitToken>() != nullptr) {
230 Interest interest2 = interest; // make a copy to preserve tag on original packet
231 interest2.removeTag<lp::PitToken>();
232 return m_forwarder.onOutgoingInterest(interest2, egress, pitEntry);
233 }
234 return m_forwarder.onOutgoingInterest(interest, egress, pitEntry);
235}
236
Eric Newberry2377ada2020-09-28 22:40:14 -0700237bool
Davide Pesavento0498ce82021-06-14 02:02:21 -0400238Strategy::sendData(const Data& data, Face& egress, const shared_ptr<pit::Entry>& pitEntry)
Teng Liang43bb2312018-03-26 04:16:42 -0700239{
240 BOOST_ASSERT(pitEntry->getInterest().matchesData(data));
241
Junxiao Shi606d5dd2019-09-23 12:47:44 -0600242 shared_ptr<lp::PitToken> pitToken;
Teng Liangebc20f62020-06-23 16:55:20 -0700243 auto inRecord = pitEntry->getInRecord(egress);
Junxiao Shi606d5dd2019-09-23 12:47:44 -0600244 if (inRecord != pitEntry->in_end()) {
245 pitToken = inRecord->getInterest().getTag<lp::PitToken>();
246 }
247
ashiqopuc7079482019-02-20 05:34:37 +0000248 // delete the PIT entry's in-record based on egress,
Teng Liangebc20f62020-06-23 16:55:20 -0700249 // since the Data is sent to the face from which the Interest was received
250 pitEntry->deleteInRecord(egress);
Teng Liang43bb2312018-03-26 04:16:42 -0700251
Junxiao Shi606d5dd2019-09-23 12:47:44 -0600252 if (pitToken != nullptr) {
253 Data data2 = data; // make a copy so each downstream can get a different PIT token
254 data2.setTag(pitToken);
Eric Newberry2377ada2020-09-28 22:40:14 -0700255 return m_forwarder.onOutgoingData(data2, egress);
Junxiao Shi606d5dd2019-09-23 12:47:44 -0600256 }
Eric Newberry2377ada2020-09-28 22:40:14 -0700257 return m_forwarder.onOutgoingData(data, egress);
Teng Liang43bb2312018-03-26 04:16:42 -0700258}
259
260void
Davide Pesavento0498ce82021-06-14 02:02:21 -0400261Strategy::sendDataToAll(const Data& data, const shared_ptr<pit::Entry>& pitEntry, const Face& inFace)
Teng Liang43bb2312018-03-26 04:16:42 -0700262{
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000263 std::set<Face*> pendingDownstreams;
Teng Liang43bb2312018-03-26 04:16:42 -0700264 auto now = time::steady_clock::now();
265
266 // remember pending downstreams
Davide Pesavento0498ce82021-06-14 02:02:21 -0400267 for (const auto& inRecord : pitEntry->getInRecords()) {
Teng Liang43bb2312018-03-26 04:16:42 -0700268 if (inRecord.getExpiry() > now) {
Teng Liangebc20f62020-06-23 16:55:20 -0700269 if (inRecord.getFace().getId() == inFace.getId() &&
Teng Liang43bb2312018-03-26 04:16:42 -0700270 inRecord.getFace().getLinkType() != ndn::nfd::LINK_TYPE_AD_HOC) {
271 continue;
272 }
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000273 pendingDownstreams.emplace(&inRecord.getFace());
Teng Liang43bb2312018-03-26 04:16:42 -0700274 }
275 }
276
ashiqopuc7079482019-02-20 05:34:37 +0000277 for (const auto& pendingDownstream : pendingDownstreams) {
Davide Pesavento0498ce82021-06-14 02:02:21 -0400278 this->sendData(data, *pendingDownstream, pitEntry);
Teng Liang43bb2312018-03-26 04:16:42 -0700279 }
280}
281
282void
Davide Pesavento0498ce82021-06-14 02:02:21 -0400283Strategy::sendNacks(const lp::NackHeader& header, const shared_ptr<pit::Entry>& pitEntry,
Teng Liangebc20f62020-06-23 16:55:20 -0700284 std::initializer_list<const Face*> exceptFaces)
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700285{
286 // populate downstreams with all downstreams faces
Teng Liangebc20f62020-06-23 16:55:20 -0700287 std::unordered_set<Face*> downstreams;
Davide Pesavento0498ce82021-06-14 02:02:21 -0400288 std::transform(pitEntry->in_begin(), pitEntry->in_end(),
289 std::inserter(downstreams, downstreams.end()),
290 [] (const auto& inR) { return &inR.getFace(); });
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700291
Davide Pesavento0498ce82021-06-14 02:02:21 -0400292 // remove excluded faces
Teng Liangebc20f62020-06-23 16:55:20 -0700293 for (auto exceptFace : exceptFaces) {
294 downstreams.erase(const_cast<Face*>(exceptFace));
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700295 }
296
297 // send Nacks
Teng Liangebc20f62020-06-23 16:55:20 -0700298 for (auto downstream : downstreams) {
Davide Pesavento0498ce82021-06-14 02:02:21 -0400299 this->sendNack(header, *downstream, pitEntry);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700300 }
Junxiao Shi4846f372016-04-05 13:39:30 -0700301 // warning: don't loop on pitEntry->getInRecords(), because in-record is deleted when sending Nack
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700302}
Junxiao Shid3c792f2014-01-30 00:46:13 -0700303
Junxiao Shicf0f3ce2016-09-02 13:01:59 +0000304const fib::Entry&
305Strategy::lookupFib(const pit::Entry& pitEntry) const
306{
307 const Fib& fib = m_forwarder.getFib();
Junxiao Shicf0f3ce2016-09-02 13:01:59 +0000308
309 const Interest& interest = pitEntry.getInterest();
Junxiao Shifc2e13d2017-07-25 02:08:48 +0000310 // has forwarding hint?
311 if (interest.getForwardingHint().empty()) {
Junxiao Shicf0f3ce2016-09-02 13:01:59 +0000312 // FIB lookup with Interest name
313 const fib::Entry& fibEntry = fib.findLongestPrefixMatch(pitEntry);
Junxiao Shifc2e13d2017-07-25 02:08:48 +0000314 NFD_LOG_TRACE("lookupFib noForwardingHint found=" << fibEntry.getPrefix());
Junxiao Shicf0f3ce2016-09-02 13:01:59 +0000315 return fibEntry;
316 }
317
Davide Pesavento412c9822021-07-02 00:21:05 -0400318 const auto& fh = interest.getForwardingHint();
Junxiao Shifc2e13d2017-07-25 02:08:48 +0000319 // Forwarding hint should have been stripped by incoming Interest pipeline when reaching producer region
320 BOOST_ASSERT(!m_forwarder.getNetworkRegionTable().isInProducerRegion(fh));
Junxiao Shicf0f3ce2016-09-02 13:01:59 +0000321
Junxiao Shifc2e13d2017-07-25 02:08:48 +0000322 const fib::Entry* fibEntry = nullptr;
Davide Pesavento412c9822021-07-02 00:21:05 -0400323 for (const auto& delegation : fh) {
Junxiao Shi727b6102022-01-11 18:40:04 +0000324 fibEntry = &fib.findLongestPrefixMatch(delegation);
Junxiao Shifc2e13d2017-07-25 02:08:48 +0000325 if (fibEntry->hasNextHops()) {
Ashlesh Gawande1ef93d02022-04-08 00:25:06 -0400326 if (fibEntry->getPrefix().empty()) {
Junxiao Shifc2e13d2017-07-25 02:08:48 +0000327 // in consumer region, return the default route
328 NFD_LOG_TRACE("lookupFib inConsumerRegion found=" << fibEntry->getPrefix());
329 }
330 else {
331 // in default-free zone, use the first delegation that finds a FIB entry
Junxiao Shi727b6102022-01-11 18:40:04 +0000332 NFD_LOG_TRACE("lookupFib delegation=" << delegation << " found=" << fibEntry->getPrefix());
Junxiao Shifc2e13d2017-07-25 02:08:48 +0000333 }
334 return *fibEntry;
Junxiao Shicf0f3ce2016-09-02 13:01:59 +0000335 }
Ashlesh Gawande1ef93d02022-04-08 00:25:06 -0400336 BOOST_ASSERT(fibEntry->getPrefix().empty()); // only ndn:/ FIB entry can have zero nexthop
Junxiao Shicf0f3ce2016-09-02 13:01:59 +0000337 }
Ashlesh Gawande1ef93d02022-04-08 00:25:06 -0400338 BOOST_ASSERT(fibEntry != nullptr && fibEntry->getPrefix().empty());
Junxiao Shifc2e13d2017-07-25 02:08:48 +0000339 return *fibEntry; // only occurs if no delegation finds a FIB nexthop
Junxiao Shicf0f3ce2016-09-02 13:01:59 +0000340}
341
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700342} // namespace fw
Junxiao Shid3c792f2014-01-30 00:46:13 -0700343} // namespace nfd