Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Junxiao Shi | 4846f37 | 2016-04-05 13:39:30 -0700 | [diff] [blame] | 3 | * Copyright (c) 2014-2016, Regents of the University of California, |
Junxiao Shi | 3535396 | 2015-01-08 09:13:47 -0700 | [diff] [blame] | 4 | * 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 Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 10 | * |
| 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 Shi | 82e7f58 | 2014-09-07 15:15:40 -0700 | [diff] [blame] | 24 | */ |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 25 | |
| 26 | #include "strategy.hpp" |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 27 | #include "forwarder.hpp" |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 28 | #include "core/logger.hpp" |
Junxiao Shi | c5f651f | 2016-11-17 22:58:12 +0000 | [diff] [blame] | 29 | #include "core/random.hpp" |
Junxiao Shi | c34d167 | 2016-12-09 15:57:59 +0000 | [diff] [blame] | 30 | #include <boost/range/adaptor/map.hpp> |
| 31 | #include <boost/range/algorithm/copy.hpp> |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 32 | |
| 33 | namespace nfd { |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 34 | namespace fw { |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 35 | |
Junxiao Shi | 679e927 | 2014-02-15 20:10:21 -0700 | [diff] [blame] | 36 | NFD_LOG_INIT("Strategy"); |
| 37 | |
Junxiao Shi | c34d167 | 2016-12-09 15:57:59 +0000 | [diff] [blame] | 38 | Strategy::Registry& |
| 39 | Strategy::getRegistry() |
| 40 | { |
| 41 | static Registry registry; |
| 42 | return registry; |
| 43 | } |
| 44 | |
| 45 | Strategy::Registry::const_iterator |
Junxiao Shi | 18739c4 | 2016-12-22 08:03:00 +0000 | [diff] [blame] | 46 | Strategy::find(const Name& instanceName) |
Junxiao Shi | c34d167 | 2016-12-09 15:57:59 +0000 | [diff] [blame] | 47 | { |
| 48 | const Registry& registry = getRegistry(); |
Junxiao Shi | 18739c4 | 2016-12-22 08:03:00 +0000 | [diff] [blame] | 49 | ParsedInstanceName parsed = parseInstanceName(instanceName); |
| 50 | |
Junxiao Shi | 18739c4 | 2016-12-22 08:03:00 +0000 | [diff] [blame] | 51 | if (parsed.version) { |
Junxiao Shi | 91f6ee0 | 2016-12-29 21:44:44 +0000 | [diff] [blame] | 52 | // 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 Shi | 18739c4 | 2016-12-22 08:03:00 +0000 | [diff] [blame] | 64 | } |
| 65 | |
Junxiao Shi | 91f6ee0 | 2016-12-29 21:44:44 +0000 | [diff] [blame] | 66 | // 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 Shi | c34d167 | 2016-12-09 15:57:59 +0000 | [diff] [blame] | 76 | } |
| 77 | } |
Junxiao Shi | 91f6ee0 | 2016-12-29 21:44:44 +0000 | [diff] [blame] | 78 | |
| 79 | NFD_LOG_TRACE("find " << instanceName << " unversioned not-found"); |
| 80 | return registry.end(); |
Junxiao Shi | c34d167 | 2016-12-09 15:57:59 +0000 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | bool |
Junxiao Shi | 18739c4 | 2016-12-22 08:03:00 +0000 | [diff] [blame] | 84 | Strategy::canCreate(const Name& instanceName) |
Junxiao Shi | c34d167 | 2016-12-09 15:57:59 +0000 | [diff] [blame] | 85 | { |
Junxiao Shi | 18739c4 | 2016-12-22 08:03:00 +0000 | [diff] [blame] | 86 | return Strategy::find(instanceName) != getRegistry().end(); |
Junxiao Shi | c34d167 | 2016-12-09 15:57:59 +0000 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | unique_ptr<Strategy> |
Junxiao Shi | 18739c4 | 2016-12-22 08:03:00 +0000 | [diff] [blame] | 90 | Strategy::create(const Name& instanceName, Forwarder& forwarder) |
Junxiao Shi | c34d167 | 2016-12-09 15:57:59 +0000 | [diff] [blame] | 91 | { |
Junxiao Shi | 18739c4 | 2016-12-22 08:03:00 +0000 | [diff] [blame] | 92 | auto found = Strategy::find(instanceName); |
Junxiao Shi | c34d167 | 2016-12-09 15:57:59 +0000 | [diff] [blame] | 93 | if (found == getRegistry().end()) { |
Junxiao Shi | 18739c4 | 2016-12-22 08:03:00 +0000 | [diff] [blame] | 94 | NFD_LOG_DEBUG("create " << instanceName << " not-found"); |
Junxiao Shi | c34d167 | 2016-12-09 15:57:59 +0000 | [diff] [blame] | 95 | return nullptr; |
| 96 | } |
| 97 | |
Junxiao Shi | 18739c4 | 2016-12-22 08:03:00 +0000 | [diff] [blame] | 98 | unique_ptr<Strategy> instance = found->second(forwarder, instanceName); |
| 99 | NFD_LOG_DEBUG("create " << instanceName << " found=" << found->first << |
| 100 | " created=" << instance->getInstanceName()); |
| 101 | BOOST_ASSERT(!instance->getInstanceName().empty()); |
| 102 | return instance; |
Junxiao Shi | c34d167 | 2016-12-09 15:57:59 +0000 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | std::set<Name> |
| 106 | Strategy::listRegistered() |
| 107 | { |
| 108 | std::set<Name> strategyNames; |
| 109 | boost::copy(getRegistry() | boost::adaptors::map_keys, |
| 110 | std::inserter(strategyNames, strategyNames.end())); |
| 111 | return strategyNames; |
| 112 | } |
| 113 | |
Junxiao Shi | 18739c4 | 2016-12-22 08:03:00 +0000 | [diff] [blame] | 114 | Strategy::ParsedInstanceName |
| 115 | Strategy::parseInstanceName(const Name& input) |
| 116 | { |
| 117 | for (ssize_t i = input.size() - 1; i > 0; --i) { |
| 118 | if (input[i].isVersion()) { |
| 119 | return {input.getPrefix(i + 1), input[i].toVersion(), input.getSubName(i + 1)}; |
| 120 | } |
| 121 | } |
| 122 | return {input, ndn::nullopt, PartialName()}; |
| 123 | } |
| 124 | |
| 125 | Name |
| 126 | Strategy::makeInstanceName(const Name& input, const Name& strategyName) |
| 127 | { |
| 128 | BOOST_ASSERT(strategyName.at(-1).isVersion()); |
| 129 | |
| 130 | bool hasVersion = std::any_of(input.rbegin(), input.rend(), |
| 131 | [] (const name::Component& comp) { return comp.isVersion(); }); |
| 132 | return hasVersion ? input : Name(input).append(strategyName.at(-1)); |
| 133 | } |
| 134 | |
| 135 | Strategy::Strategy(Forwarder& forwarder) |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 136 | : afterAddFace(forwarder.getFaceTable().afterAdd) |
| 137 | , beforeRemoveFace(forwarder.getFaceTable().beforeRemove) |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 138 | , m_forwarder(forwarder) |
Junxiao Shi | 15e98b0 | 2016-08-12 11:21:44 +0000 | [diff] [blame] | 139 | , m_measurements(m_forwarder.getMeasurements(), m_forwarder.getStrategyChoice(), *this) |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 140 | { |
| 141 | } |
| 142 | |
Junxiao Shi | 15e98b0 | 2016-08-12 11:21:44 +0000 | [diff] [blame] | 143 | Strategy::~Strategy() = default; |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 144 | |
Junxiao Shi | 679e927 | 2014-02-15 20:10:21 -0700 | [diff] [blame] | 145 | void |
Junxiao Shi | 15e98b0 | 2016-08-12 11:21:44 +0000 | [diff] [blame] | 146 | Strategy::beforeSatisfyInterest(const shared_ptr<pit::Entry>& pitEntry, |
Junxiao Shi | 82e7f58 | 2014-09-07 15:15:40 -0700 | [diff] [blame] | 147 | const Face& inFace, const Data& data) |
Junxiao Shi | 22be22c | 2014-02-16 22:53:48 -0700 | [diff] [blame] | 148 | { |
Junxiao Shi | 82e7f58 | 2014-09-07 15:15:40 -0700 | [diff] [blame] | 149 | NFD_LOG_DEBUG("beforeSatisfyInterest pitEntry=" << pitEntry->getName() << |
Junxiao Shi | 15e98b0 | 2016-08-12 11:21:44 +0000 | [diff] [blame] | 150 | " inFace=" << inFace.getId() << " data=" << data.getName()); |
Junxiao Shi | 22be22c | 2014-02-16 22:53:48 -0700 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | void |
Junxiao Shi | 15e98b0 | 2016-08-12 11:21:44 +0000 | [diff] [blame] | 154 | Strategy::beforeExpirePendingInterest(const shared_ptr<pit::Entry>& pitEntry) |
Junxiao Shi | 679e927 | 2014-02-15 20:10:21 -0700 | [diff] [blame] | 155 | { |
| 156 | NFD_LOG_DEBUG("beforeExpirePendingInterest pitEntry=" << pitEntry->getName()); |
| 157 | } |
| 158 | |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 159 | void |
| 160 | Strategy::afterReceiveNack(const Face& inFace, const lp::Nack& nack, |
Junxiao Shi | 15e98b0 | 2016-08-12 11:21:44 +0000 | [diff] [blame] | 161 | const shared_ptr<pit::Entry>& pitEntry) |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 162 | { |
| 163 | NFD_LOG_DEBUG("afterReceiveNack inFace=" << inFace.getId() << |
| 164 | " pitEntry=" << pitEntry->getName()); |
| 165 | } |
| 166 | |
| 167 | void |
Junxiao Shi | b9420cf | 2016-08-13 04:38:52 +0000 | [diff] [blame] | 168 | Strategy::sendNacks(const shared_ptr<pit::Entry>& pitEntry, const lp::NackHeader& header, |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 169 | std::initializer_list<const Face*> exceptFaces) |
| 170 | { |
| 171 | // populate downstreams with all downstreams faces |
| 172 | std::unordered_set<const Face*> downstreams; |
Junxiao Shi | 4846f37 | 2016-04-05 13:39:30 -0700 | [diff] [blame] | 173 | std::transform(pitEntry->in_begin(), pitEntry->in_end(), std::inserter(downstreams, downstreams.end()), |
Junxiao Shi | 9cff779 | 2016-08-01 21:45:11 +0000 | [diff] [blame] | 174 | [] (const pit::InRecord& inR) { return &inR.getFace(); }); |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 175 | |
| 176 | // delete excluded faces |
Junxiao Shi | 9cff779 | 2016-08-01 21:45:11 +0000 | [diff] [blame] | 177 | // .erase in a loop is more efficient than std::set_difference because that requires sorted range |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 178 | for (const Face* exceptFace : exceptFaces) { |
| 179 | downstreams.erase(exceptFace); |
| 180 | } |
| 181 | |
| 182 | // send Nacks |
| 183 | for (const Face* downstream : downstreams) { |
| 184 | this->sendNack(pitEntry, *downstream, header); |
| 185 | } |
Junxiao Shi | 4846f37 | 2016-04-05 13:39:30 -0700 | [diff] [blame] | 186 | // warning: don't loop on pitEntry->getInRecords(), because in-record is deleted when sending Nack |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 187 | } |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 188 | |
Junxiao Shi | cf0f3ce | 2016-09-02 13:01:59 +0000 | [diff] [blame] | 189 | const fib::Entry& |
| 190 | Strategy::lookupFib(const pit::Entry& pitEntry) const |
| 191 | { |
| 192 | const Fib& fib = m_forwarder.getFib(); |
| 193 | const NetworkRegionTable& nrt = m_forwarder.getNetworkRegionTable(); |
| 194 | |
| 195 | const Interest& interest = pitEntry.getInterest(); |
| 196 | // has Link object? |
| 197 | if (!interest.hasLink()) { |
| 198 | // FIB lookup with Interest name |
| 199 | const fib::Entry& fibEntry = fib.findLongestPrefixMatch(pitEntry); |
| 200 | NFD_LOG_TRACE("lookupFib noLinkObject found=" << fibEntry.getPrefix()); |
| 201 | return fibEntry; |
| 202 | } |
| 203 | |
| 204 | const Link& link = interest.getLink(); |
| 205 | |
| 206 | // in producer region? |
| 207 | if (nrt.isInProducerRegion(link)) { |
| 208 | // FIB lookup with Interest name |
| 209 | const fib::Entry& fibEntry = fib.findLongestPrefixMatch(pitEntry); |
| 210 | NFD_LOG_TRACE("lookupFib inProducerRegion found=" << fibEntry.getPrefix()); |
| 211 | return fibEntry; |
| 212 | } |
| 213 | |
| 214 | // has SelectedDelegation? |
| 215 | if (interest.hasSelectedDelegation()) { |
| 216 | // FIB lookup with SelectedDelegation |
| 217 | Name selectedDelegation = interest.getSelectedDelegation(); |
| 218 | const fib::Entry& fibEntry = fib.findLongestPrefixMatch(selectedDelegation); |
| 219 | NFD_LOG_TRACE("lookupFib hasSelectedDelegation=" << selectedDelegation << " found=" << fibEntry.getPrefix()); |
| 220 | return fibEntry; |
| 221 | } |
| 222 | |
| 223 | // FIB lookup with first delegation Name |
| 224 | const fib::Entry& fibEntry0 = fib.findLongestPrefixMatch(link.getDelegations().begin()->second); |
| 225 | // in default-free zone? |
| 226 | bool isDefaultFreeZone = !(fibEntry0.getPrefix().size() == 0 && fibEntry0.hasNextHops()); |
| 227 | if (!isDefaultFreeZone) { |
| 228 | NFD_LOG_TRACE("lookupFib inConsumerRegion found=" << fibEntry0.getPrefix()); |
| 229 | return fibEntry0; |
| 230 | } |
| 231 | |
| 232 | // choose and set SelectedDelegation |
| 233 | for (const std::pair<uint32_t, Name>& delegation : link.getDelegations()) { |
| 234 | const Name& delegationName = delegation.second; |
| 235 | const fib::Entry& fibEntry = fib.findLongestPrefixMatch(delegationName); |
| 236 | if (fibEntry.hasNextHops()) { |
| 237 | /// \todo Don't modify in-record Interests. |
| 238 | /// Set SelectedDelegation in outgoing Interest pipeline. |
| 239 | std::for_each(pitEntry.in_begin(), pitEntry.in_end(), |
| 240 | [&delegationName] (const pit::InRecord& inR) { |
| 241 | const_cast<Interest&>(inR.getInterest()).setSelectedDelegation(delegationName); |
| 242 | }); |
| 243 | NFD_LOG_TRACE("lookupFib enterDefaultFreeZone setSelectedDelegation=" << delegationName); |
| 244 | return fibEntry; |
| 245 | } |
| 246 | } |
| 247 | BOOST_ASSERT(false); |
| 248 | return fibEntry0; |
| 249 | } |
| 250 | |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 251 | } // namespace fw |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 252 | } // namespace nfd |