blob: 300912316e0062c7bf36bd678500c3e95ea61306 [file] [log] [blame]
Junxiao Shid3c792f2014-01-30 00:46:13 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shi4846f372016-04-05 13:39:30 -07003 * Copyright (c) 2014-2016, 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"
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
Junxiao Shi679e9272014-02-15 20:10:21 -070036NFD_LOG_INIT("Strategy");
37
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
51 ///\todo #3868 accommodate parameters
52 if (parsed.version) {
53 NFD_LOG_TRACE("find " << instanceName <<
54 " strategyName=" << parsed.strategyName << " versioned");
55 ///\todo #3868 if exact version unavailable, choose closest higher version
56 return registry.find(parsed.strategyName);
57 }
58
59 NFD_LOG_TRACE("find " << instanceName <<
60 " strategyName=" << parsed.strategyName << " unversioned");
Junxiao Shic34d1672016-12-09 15:57:59 +000061 Registry::const_iterator candidate = registry.end();
Junxiao Shi18739c42016-12-22 08:03:00 +000062 for (auto it = registry.lower_bound(parsed.strategyName);
63 it != registry.end() && parsed.strategyName.isPrefixOf(it->first); ++it) {
64 if (it->first.size() == parsed.strategyName.size() + 1) { // only one extra component: version
65 candidate = it;
Junxiao Shic34d1672016-12-09 15:57:59 +000066 }
67 }
68 return candidate;
Junxiao Shic34d1672016-12-09 15:57:59 +000069}
70
71bool
Junxiao Shi18739c42016-12-22 08:03:00 +000072Strategy::canCreate(const Name& instanceName)
Junxiao Shic34d1672016-12-09 15:57:59 +000073{
Junxiao Shi18739c42016-12-22 08:03:00 +000074 return Strategy::find(instanceName) != getRegistry().end();
Junxiao Shic34d1672016-12-09 15:57:59 +000075}
76
77unique_ptr<Strategy>
Junxiao Shi18739c42016-12-22 08:03:00 +000078Strategy::create(const Name& instanceName, Forwarder& forwarder)
Junxiao Shic34d1672016-12-09 15:57:59 +000079{
Junxiao Shi18739c42016-12-22 08:03:00 +000080 auto found = Strategy::find(instanceName);
Junxiao Shic34d1672016-12-09 15:57:59 +000081 if (found == getRegistry().end()) {
Junxiao Shi18739c42016-12-22 08:03:00 +000082 NFD_LOG_DEBUG("create " << instanceName << " not-found");
Junxiao Shic34d1672016-12-09 15:57:59 +000083 return nullptr;
84 }
85
Junxiao Shi18739c42016-12-22 08:03:00 +000086 unique_ptr<Strategy> instance = found->second(forwarder, instanceName);
87 NFD_LOG_DEBUG("create " << instanceName << " found=" << found->first <<
88 " created=" << instance->getInstanceName());
89 BOOST_ASSERT(!instance->getInstanceName().empty());
90 return instance;
Junxiao Shic34d1672016-12-09 15:57:59 +000091}
92
93std::set<Name>
94Strategy::listRegistered()
95{
96 std::set<Name> strategyNames;
97 boost::copy(getRegistry() | boost::adaptors::map_keys,
98 std::inserter(strategyNames, strategyNames.end()));
99 return strategyNames;
100}
101
Junxiao Shi18739c42016-12-22 08:03:00 +0000102Strategy::ParsedInstanceName
103Strategy::parseInstanceName(const Name& input)
104{
105 for (ssize_t i = input.size() - 1; i > 0; --i) {
106 if (input[i].isVersion()) {
107 return {input.getPrefix(i + 1), input[i].toVersion(), input.getSubName(i + 1)};
108 }
109 }
110 return {input, ndn::nullopt, PartialName()};
111}
112
113Name
114Strategy::makeInstanceName(const Name& input, const Name& strategyName)
115{
116 BOOST_ASSERT(strategyName.at(-1).isVersion());
117
118 bool hasVersion = std::any_of(input.rbegin(), input.rend(),
119 [] (const name::Component& comp) { return comp.isVersion(); });
120 return hasVersion ? input : Name(input).append(strategyName.at(-1));
121}
122
123Strategy::Strategy(Forwarder& forwarder)
Junxiao Shicde37ad2015-12-24 01:02:05 -0700124 : afterAddFace(forwarder.getFaceTable().afterAdd)
125 , beforeRemoveFace(forwarder.getFaceTable().beforeRemove)
Junxiao Shibb5105f2014-03-03 12:06:45 -0700126 , m_forwarder(forwarder)
Junxiao Shi15e98b02016-08-12 11:21:44 +0000127 , m_measurements(m_forwarder.getMeasurements(), m_forwarder.getStrategyChoice(), *this)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700128{
129}
130
Junxiao Shi15e98b02016-08-12 11:21:44 +0000131Strategy::~Strategy() = default;
Junxiao Shid3c792f2014-01-30 00:46:13 -0700132
Junxiao Shi679e9272014-02-15 20:10:21 -0700133void
Junxiao Shi15e98b02016-08-12 11:21:44 +0000134Strategy::beforeSatisfyInterest(const shared_ptr<pit::Entry>& pitEntry,
Junxiao Shi82e7f582014-09-07 15:15:40 -0700135 const Face& inFace, const Data& data)
Junxiao Shi22be22c2014-02-16 22:53:48 -0700136{
Junxiao Shi82e7f582014-09-07 15:15:40 -0700137 NFD_LOG_DEBUG("beforeSatisfyInterest pitEntry=" << pitEntry->getName() <<
Junxiao Shi15e98b02016-08-12 11:21:44 +0000138 " inFace=" << inFace.getId() << " data=" << data.getName());
Junxiao Shi22be22c2014-02-16 22:53:48 -0700139}
140
141void
Junxiao Shi15e98b02016-08-12 11:21:44 +0000142Strategy::beforeExpirePendingInterest(const shared_ptr<pit::Entry>& pitEntry)
Junxiao Shi679e9272014-02-15 20:10:21 -0700143{
144 NFD_LOG_DEBUG("beforeExpirePendingInterest pitEntry=" << pitEntry->getName());
145}
146
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700147void
148Strategy::afterReceiveNack(const Face& inFace, const lp::Nack& nack,
Junxiao Shi15e98b02016-08-12 11:21:44 +0000149 const shared_ptr<pit::Entry>& pitEntry)
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700150{
151 NFD_LOG_DEBUG("afterReceiveNack inFace=" << inFace.getId() <<
152 " pitEntry=" << pitEntry->getName());
153}
154
155void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000156Strategy::sendNacks(const shared_ptr<pit::Entry>& pitEntry, const lp::NackHeader& header,
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700157 std::initializer_list<const Face*> exceptFaces)
158{
159 // populate downstreams with all downstreams faces
160 std::unordered_set<const Face*> downstreams;
Junxiao Shi4846f372016-04-05 13:39:30 -0700161 std::transform(pitEntry->in_begin(), pitEntry->in_end(), std::inserter(downstreams, downstreams.end()),
Junxiao Shi9cff7792016-08-01 21:45:11 +0000162 [] (const pit::InRecord& inR) { return &inR.getFace(); });
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700163
164 // delete excluded faces
Junxiao Shi9cff7792016-08-01 21:45:11 +0000165 // .erase in a loop is more efficient than std::set_difference because that requires sorted range
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700166 for (const Face* exceptFace : exceptFaces) {
167 downstreams.erase(exceptFace);
168 }
169
170 // send Nacks
171 for (const Face* downstream : downstreams) {
172 this->sendNack(pitEntry, *downstream, header);
173 }
Junxiao Shi4846f372016-04-05 13:39:30 -0700174 // warning: don't loop on pitEntry->getInRecords(), because in-record is deleted when sending Nack
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700175}
Junxiao Shid3c792f2014-01-30 00:46:13 -0700176
Junxiao Shicf0f3ce2016-09-02 13:01:59 +0000177const fib::Entry&
178Strategy::lookupFib(const pit::Entry& pitEntry) const
179{
180 const Fib& fib = m_forwarder.getFib();
181 const NetworkRegionTable& nrt = m_forwarder.getNetworkRegionTable();
182
183 const Interest& interest = pitEntry.getInterest();
184 // has Link object?
185 if (!interest.hasLink()) {
186 // FIB lookup with Interest name
187 const fib::Entry& fibEntry = fib.findLongestPrefixMatch(pitEntry);
188 NFD_LOG_TRACE("lookupFib noLinkObject found=" << fibEntry.getPrefix());
189 return fibEntry;
190 }
191
192 const Link& link = interest.getLink();
193
194 // in producer region?
195 if (nrt.isInProducerRegion(link)) {
196 // FIB lookup with Interest name
197 const fib::Entry& fibEntry = fib.findLongestPrefixMatch(pitEntry);
198 NFD_LOG_TRACE("lookupFib inProducerRegion found=" << fibEntry.getPrefix());
199 return fibEntry;
200 }
201
202 // has SelectedDelegation?
203 if (interest.hasSelectedDelegation()) {
204 // FIB lookup with SelectedDelegation
205 Name selectedDelegation = interest.getSelectedDelegation();
206 const fib::Entry& fibEntry = fib.findLongestPrefixMatch(selectedDelegation);
207 NFD_LOG_TRACE("lookupFib hasSelectedDelegation=" << selectedDelegation << " found=" << fibEntry.getPrefix());
208 return fibEntry;
209 }
210
211 // FIB lookup with first delegation Name
212 const fib::Entry& fibEntry0 = fib.findLongestPrefixMatch(link.getDelegations().begin()->second);
213 // in default-free zone?
214 bool isDefaultFreeZone = !(fibEntry0.getPrefix().size() == 0 && fibEntry0.hasNextHops());
215 if (!isDefaultFreeZone) {
216 NFD_LOG_TRACE("lookupFib inConsumerRegion found=" << fibEntry0.getPrefix());
217 return fibEntry0;
218 }
219
220 // choose and set SelectedDelegation
221 for (const std::pair<uint32_t, Name>& delegation : link.getDelegations()) {
222 const Name& delegationName = delegation.second;
223 const fib::Entry& fibEntry = fib.findLongestPrefixMatch(delegationName);
224 if (fibEntry.hasNextHops()) {
225 /// \todo Don't modify in-record Interests.
226 /// Set SelectedDelegation in outgoing Interest pipeline.
227 std::for_each(pitEntry.in_begin(), pitEntry.in_end(),
228 [&delegationName] (const pit::InRecord& inR) {
229 const_cast<Interest&>(inR.getInterest()).setSelectedDelegation(delegationName);
230 });
231 NFD_LOG_TRACE("lookupFib enterDefaultFreeZone setSelectedDelegation=" << delegationName);
232 return fibEntry;
233 }
234 }
235 BOOST_ASSERT(false);
236 return fibEntry0;
237}
238
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700239} // namespace fw
Junxiao Shid3c792f2014-01-30 00:46:13 -0700240} // namespace nfd