blob: ae8a17c8a8a7274418d1cdcfce4c7aec28ba0146 [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
46Strategy::find(const Name& strategyName)
47{
48 const Registry& registry = getRegistry();
49 Registry::const_iterator candidate = registry.end();
50 for (auto it = registry.lower_bound(strategyName);
51 it != registry.end() && strategyName.isPrefixOf(it->first); ++it) {
52 switch (it->first.size() - strategyName.size()) {
53 case 0: // exact match
54 return it;
55 case 1: // unversioned strategyName matches versioned strategy
56 candidate = it;
57 break;
58 }
59 }
60 return candidate;
61 ///\todo #3868 if exact version unavailable, choose closest higher version
62 ///\todo #3868 allow parameter component
63}
64
65bool
66Strategy::canCreate(const Name& strategyName)
67{
68 return Strategy::find(strategyName) != getRegistry().end();
69}
70
71unique_ptr<Strategy>
72Strategy::create(const Name& strategyName, Forwarder& forwarder)
73{
74 auto found = Strategy::find(strategyName);
75 if (found == getRegistry().end()) {
76 NFD_LOG_DEBUG("create " << strategyName << " not-found");
77 return nullptr;
78 }
79
80 NFD_LOG_DEBUG("create " << strategyName << " found=" << found->first);
81 ///\todo #3868 pass parameters to strategy constructor
82 return found->second(forwarder, found->first);
83}
84
85std::set<Name>
86Strategy::listRegistered()
87{
88 std::set<Name> strategyNames;
89 boost::copy(getRegistry() | boost::adaptors::map_keys,
90 std::inserter(strategyNames, strategyNames.end()));
91 return strategyNames;
92}
93
Junxiao Shibb5105f2014-03-03 12:06:45 -070094Strategy::Strategy(Forwarder& forwarder, const Name& name)
Junxiao Shicde37ad2015-12-24 01:02:05 -070095 : afterAddFace(forwarder.getFaceTable().afterAdd)
96 , beforeRemoveFace(forwarder.getFaceTable().beforeRemove)
Junxiao Shi49e11e72014-12-14 19:46:05 -070097 , m_name(name)
Junxiao Shibb5105f2014-03-03 12:06:45 -070098 , m_forwarder(forwarder)
Junxiao Shi15e98b02016-08-12 11:21:44 +000099 , m_measurements(m_forwarder.getMeasurements(), m_forwarder.getStrategyChoice(), *this)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700100{
101}
102
Junxiao Shi15e98b02016-08-12 11:21:44 +0000103Strategy::~Strategy() = default;
Junxiao Shid3c792f2014-01-30 00:46:13 -0700104
Junxiao Shi679e9272014-02-15 20:10:21 -0700105void
Junxiao Shi15e98b02016-08-12 11:21:44 +0000106Strategy::beforeSatisfyInterest(const shared_ptr<pit::Entry>& pitEntry,
Junxiao Shi82e7f582014-09-07 15:15:40 -0700107 const Face& inFace, const Data& data)
Junxiao Shi22be22c2014-02-16 22:53:48 -0700108{
Junxiao Shi82e7f582014-09-07 15:15:40 -0700109 NFD_LOG_DEBUG("beforeSatisfyInterest pitEntry=" << pitEntry->getName() <<
Junxiao Shi15e98b02016-08-12 11:21:44 +0000110 " inFace=" << inFace.getId() << " data=" << data.getName());
Junxiao Shi22be22c2014-02-16 22:53:48 -0700111}
112
113void
Junxiao Shi15e98b02016-08-12 11:21:44 +0000114Strategy::beforeExpirePendingInterest(const shared_ptr<pit::Entry>& pitEntry)
Junxiao Shi679e9272014-02-15 20:10:21 -0700115{
116 NFD_LOG_DEBUG("beforeExpirePendingInterest pitEntry=" << pitEntry->getName());
117}
118
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700119void
120Strategy::afterReceiveNack(const Face& inFace, const lp::Nack& nack,
Junxiao Shi15e98b02016-08-12 11:21:44 +0000121 const shared_ptr<pit::Entry>& pitEntry)
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700122{
123 NFD_LOG_DEBUG("afterReceiveNack inFace=" << inFace.getId() <<
124 " pitEntry=" << pitEntry->getName());
125}
126
127void
Junxiao Shib9420cf2016-08-13 04:38:52 +0000128Strategy::sendNacks(const shared_ptr<pit::Entry>& pitEntry, const lp::NackHeader& header,
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700129 std::initializer_list<const Face*> exceptFaces)
130{
131 // populate downstreams with all downstreams faces
132 std::unordered_set<const Face*> downstreams;
Junxiao Shi4846f372016-04-05 13:39:30 -0700133 std::transform(pitEntry->in_begin(), pitEntry->in_end(), std::inserter(downstreams, downstreams.end()),
Junxiao Shi9cff7792016-08-01 21:45:11 +0000134 [] (const pit::InRecord& inR) { return &inR.getFace(); });
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700135
136 // delete excluded faces
Junxiao Shi9cff7792016-08-01 21:45:11 +0000137 // .erase in a loop is more efficient than std::set_difference because that requires sorted range
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700138 for (const Face* exceptFace : exceptFaces) {
139 downstreams.erase(exceptFace);
140 }
141
142 // send Nacks
143 for (const Face* downstream : downstreams) {
144 this->sendNack(pitEntry, *downstream, header);
145 }
Junxiao Shi4846f372016-04-05 13:39:30 -0700146 // warning: don't loop on pitEntry->getInRecords(), because in-record is deleted when sending Nack
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700147}
Junxiao Shid3c792f2014-01-30 00:46:13 -0700148
Junxiao Shicf0f3ce2016-09-02 13:01:59 +0000149const fib::Entry&
150Strategy::lookupFib(const pit::Entry& pitEntry) const
151{
152 const Fib& fib = m_forwarder.getFib();
153 const NetworkRegionTable& nrt = m_forwarder.getNetworkRegionTable();
154
155 const Interest& interest = pitEntry.getInterest();
156 // has Link object?
157 if (!interest.hasLink()) {
158 // FIB lookup with Interest name
159 const fib::Entry& fibEntry = fib.findLongestPrefixMatch(pitEntry);
160 NFD_LOG_TRACE("lookupFib noLinkObject found=" << fibEntry.getPrefix());
161 return fibEntry;
162 }
163
164 const Link& link = interest.getLink();
165
166 // in producer region?
167 if (nrt.isInProducerRegion(link)) {
168 // FIB lookup with Interest name
169 const fib::Entry& fibEntry = fib.findLongestPrefixMatch(pitEntry);
170 NFD_LOG_TRACE("lookupFib inProducerRegion found=" << fibEntry.getPrefix());
171 return fibEntry;
172 }
173
174 // has SelectedDelegation?
175 if (interest.hasSelectedDelegation()) {
176 // FIB lookup with SelectedDelegation
177 Name selectedDelegation = interest.getSelectedDelegation();
178 const fib::Entry& fibEntry = fib.findLongestPrefixMatch(selectedDelegation);
179 NFD_LOG_TRACE("lookupFib hasSelectedDelegation=" << selectedDelegation << " found=" << fibEntry.getPrefix());
180 return fibEntry;
181 }
182
183 // FIB lookup with first delegation Name
184 const fib::Entry& fibEntry0 = fib.findLongestPrefixMatch(link.getDelegations().begin()->second);
185 // in default-free zone?
186 bool isDefaultFreeZone = !(fibEntry0.getPrefix().size() == 0 && fibEntry0.hasNextHops());
187 if (!isDefaultFreeZone) {
188 NFD_LOG_TRACE("lookupFib inConsumerRegion found=" << fibEntry0.getPrefix());
189 return fibEntry0;
190 }
191
192 // choose and set SelectedDelegation
193 for (const std::pair<uint32_t, Name>& delegation : link.getDelegations()) {
194 const Name& delegationName = delegation.second;
195 const fib::Entry& fibEntry = fib.findLongestPrefixMatch(delegationName);
196 if (fibEntry.hasNextHops()) {
197 /// \todo Don't modify in-record Interests.
198 /// Set SelectedDelegation in outgoing Interest pipeline.
199 std::for_each(pitEntry.in_begin(), pitEntry.in_end(),
200 [&delegationName] (const pit::InRecord& inR) {
201 const_cast<Interest&>(inR.getInterest()).setSelectedDelegation(delegationName);
202 });
203 NFD_LOG_TRACE("lookupFib enterDefaultFreeZone setSelectedDelegation=" << delegationName);
204 return fibEntry;
205 }
206 }
207 BOOST_ASSERT(false);
208 return fibEntry0;
209}
210
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700211} // namespace fw
Junxiao Shid3c792f2014-01-30 00:46:13 -0700212} // namespace nfd