Junxiao Shi | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | 3d2049f | 2018-01-26 23:46:06 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2014-2018, Regents of the University of California, |
Junxiao Shi | 5640ec8 | 2015-01-07 21:51:19 -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. |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 10 | * |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 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 | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 24 | */ |
| 25 | |
Junxiao Shi | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 26 | #include "cs.hpp" |
Junxiao Shi | 5640ec8 | 2015-01-07 21:51:19 -0700 | [diff] [blame] | 27 | #include "core/algorithm.hpp" |
Junxiao Shi | 3aba754 | 2016-12-24 02:42:52 +0000 | [diff] [blame] | 28 | #include "core/logger.hpp" |
Davide Pesavento | 981db80 | 2018-04-10 18:05:04 -0400 | [diff] [blame] | 29 | |
Junxiao Shi | cbc8e94 | 2016-09-06 03:17:45 +0000 | [diff] [blame] | 30 | #include <ndn-cxx/lp/tags.hpp> |
Davide Pesavento | 981db80 | 2018-04-10 18:05:04 -0400 | [diff] [blame] | 31 | #include <ndn-cxx/util/concepts.hpp> |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 32 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 33 | namespace nfd { |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 34 | namespace cs { |
Junxiao Shi | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 35 | |
Davide Pesavento | 981db80 | 2018-04-10 18:05:04 -0400 | [diff] [blame] | 36 | NDN_CXX_ASSERT_FORWARD_ITERATOR(Cs::const_iterator); |
Junxiao Shi | 3aba754 | 2016-12-24 02:42:52 +0000 | [diff] [blame] | 37 | |
Davide Pesavento | a314808 | 2018-04-12 18:21:54 -0400 | [diff] [blame] | 38 | NFD_LOG_INIT(ContentStore); |
Junxiao Shi | fc20696 | 2015-01-16 11:12:22 -0700 | [diff] [blame] | 39 | |
Chavoosh Ghasemi | 32e7609 | 2018-09-10 14:51:33 -0700 | [diff] [blame] | 40 | static unique_ptr<Policy> |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 41 | makeDefaultPolicy() |
Junxiao Shi | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 42 | { |
Chavoosh Ghasemi | 32e7609 | 2018-09-10 14:51:33 -0700 | [diff] [blame] | 43 | return Policy::create("lru"); |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 44 | } |
| 45 | |
Junxiao Shi | b4a5acd | 2016-12-07 19:59:18 +0000 | [diff] [blame] | 46 | Cs::Cs(size_t nMaxPackets) |
Junxiao Shi | 3d2049f | 2018-01-26 23:46:06 +0000 | [diff] [blame] | 47 | : m_shouldAdmit(true) |
| 48 | , m_shouldServe(true) |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 49 | { |
Junxiao Shi | b4a5acd | 2016-12-07 19:59:18 +0000 | [diff] [blame] | 50 | this->setPolicyImpl(makeDefaultPolicy()); |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 51 | m_policy->setLimit(nMaxPackets); |
Junxiao Shi | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 52 | } |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 53 | |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 54 | void |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 55 | Cs::insert(const Data& data, bool isUnsolicited) |
Junxiao Shi | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 56 | { |
Junxiao Shi | 3d2049f | 2018-01-26 23:46:06 +0000 | [diff] [blame] | 57 | if (!m_shouldAdmit || m_policy->getLimit() == 0) { |
Minsheng Zhang | ffe8bbb | 2016-03-10 13:40:37 -0700 | [diff] [blame] | 58 | return; |
| 59 | } |
Junxiao Shi | 3d2049f | 2018-01-26 23:46:06 +0000 | [diff] [blame] | 60 | NFD_LOG_DEBUG("insert " << data.getName()); |
Minsheng Zhang | ffe8bbb | 2016-03-10 13:40:37 -0700 | [diff] [blame] | 61 | |
Junxiao Shi | 0de23a2 | 2015-12-03 20:07:02 +0000 | [diff] [blame] | 62 | // recognize CachePolicy |
| 63 | shared_ptr<lp::CachePolicyTag> tag = data.getTag<lp::CachePolicyTag>(); |
| 64 | if (tag != nullptr) { |
| 65 | lp::CachePolicyType policy = tag->get().getPolicy(); |
| 66 | if (policy == lp::CachePolicyType::NO_CACHE) { |
Minsheng Zhang | ffe8bbb | 2016-03-10 13:40:37 -0700 | [diff] [blame] | 67 | return; |
Junxiao Shi | 35b16b1 | 2015-02-16 22:14:57 -0700 | [diff] [blame] | 68 | } |
| 69 | } |
| 70 | |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 71 | iterator it; |
Junxiao Shi | 3d2049f | 2018-01-26 23:46:06 +0000 | [diff] [blame] | 72 | bool isNewEntry = false; |
| 73 | std::tie(it, isNewEntry) = m_table.emplace(data.shared_from_this(), isUnsolicited); |
Junxiao Shi | fc20696 | 2015-01-16 11:12:22 -0700 | [diff] [blame] | 74 | EntryImpl& entry = const_cast<EntryImpl&>(*it); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 75 | |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 76 | entry.updateStaleTime(); |
| 77 | |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 78 | if (!isNewEntry) { // existing entry |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 79 | // XXX This doesn't forbid unsolicited Data from refreshing a solicited entry. |
| 80 | if (entry.isUnsolicited() && !isUnsolicited) { |
| 81 | entry.unsetUnsolicited(); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 82 | } |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 83 | |
| 84 | m_policy->afterRefresh(it); |
Junxiao Shi | af6569a | 2014-06-14 00:01:34 -0700 | [diff] [blame] | 85 | } |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 86 | else { |
| 87 | m_policy->afterInsert(it); |
| 88 | } |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 89 | } |
| 90 | |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 91 | void |
Junxiao Shi | 30c37ab | 2018-04-09 14:26:47 +0000 | [diff] [blame] | 92 | Cs::erase(const Name& prefix, size_t limit, const AfterEraseCallback& cb) |
| 93 | { |
| 94 | BOOST_ASSERT(static_cast<bool>(cb)); |
| 95 | |
| 96 | iterator first = m_table.lower_bound(prefix); |
| 97 | iterator last = m_table.end(); |
| 98 | if (prefix.size() > 0) { |
| 99 | last = m_table.lower_bound(prefix.getSuccessor()); |
| 100 | } |
| 101 | |
| 102 | size_t nErased = 0; |
| 103 | while (first != last && nErased < limit) { |
| 104 | m_policy->beforeErase(first); |
| 105 | first = m_table.erase(first); |
| 106 | ++nErased; |
| 107 | } |
| 108 | |
| 109 | if (cb) { |
| 110 | cb(nErased); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | void |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 115 | Cs::find(const Interest& interest, |
| 116 | const HitCallback& hitCallback, |
| 117 | const MissCallback& missCallback) const |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 118 | { |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 119 | BOOST_ASSERT(static_cast<bool>(hitCallback)); |
| 120 | BOOST_ASSERT(static_cast<bool>(missCallback)); |
| 121 | |
Junxiao Shi | 3d2049f | 2018-01-26 23:46:06 +0000 | [diff] [blame] | 122 | if (!m_shouldServe || m_policy->getLimit() == 0) { |
| 123 | missCallback(interest); |
| 124 | return; |
| 125 | } |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 126 | const Name& prefix = interest.getName(); |
| 127 | bool isRightmost = interest.getChildSelector() == 1; |
| 128 | NFD_LOG_DEBUG("find " << prefix << (isRightmost ? " R" : " L")); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 129 | |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 130 | iterator first = m_table.lower_bound(prefix); |
| 131 | iterator last = m_table.end(); |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 132 | if (prefix.size() > 0) { |
Junxiao Shi | 5640ec8 | 2015-01-07 21:51:19 -0700 | [diff] [blame] | 133 | last = m_table.lower_bound(prefix.getSuccessor()); |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 134 | } |
Junxiao Shi | 5640ec8 | 2015-01-07 21:51:19 -0700 | [diff] [blame] | 135 | |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 136 | iterator match = last; |
Junxiao Shi | 5640ec8 | 2015-01-07 21:51:19 -0700 | [diff] [blame] | 137 | if (isRightmost) { |
| 138 | match = this->findRightmost(interest, first, last); |
| 139 | } |
| 140 | else { |
| 141 | match = this->findLeftmost(interest, first, last); |
| 142 | } |
| 143 | |
| 144 | if (match == last) { |
| 145 | NFD_LOG_DEBUG(" no-match"); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 146 | missCallback(interest); |
| 147 | return; |
Junxiao Shi | 5640ec8 | 2015-01-07 21:51:19 -0700 | [diff] [blame] | 148 | } |
| 149 | NFD_LOG_DEBUG(" matching " << match->getName()); |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 150 | m_policy->beforeUse(match); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 151 | hitCallback(interest, match->getData()); |
Junxiao Shi | 5640ec8 | 2015-01-07 21:51:19 -0700 | [diff] [blame] | 152 | } |
| 153 | |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 154 | iterator |
| 155 | Cs::findLeftmost(const Interest& interest, iterator first, iterator last) const |
Junxiao Shi | 5640ec8 | 2015-01-07 21:51:19 -0700 | [diff] [blame] | 156 | { |
Davide Pesavento | e4b2238 | 2018-06-10 14:37:24 -0400 | [diff] [blame] | 157 | return std::find_if(first, last, [&interest] (const auto& entry) { return entry.canSatisfy(interest); }); |
Junxiao Shi | 5640ec8 | 2015-01-07 21:51:19 -0700 | [diff] [blame] | 158 | } |
| 159 | |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 160 | iterator |
| 161 | Cs::findRightmost(const Interest& interest, iterator first, iterator last) const |
Junxiao Shi | 5640ec8 | 2015-01-07 21:51:19 -0700 | [diff] [blame] | 162 | { |
| 163 | // Each loop visits a sub-namespace under a prefix one component longer than Interest Name. |
| 164 | // If there is a match in that sub-namespace, the leftmost match is returned; |
| 165 | // otherwise, loop continues. |
| 166 | |
| 167 | size_t interestNameLength = interest.getName().size(); |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 168 | for (iterator right = last; right != first;) { |
| 169 | iterator prev = std::prev(right); |
Junxiao Shi | 5640ec8 | 2015-01-07 21:51:19 -0700 | [diff] [blame] | 170 | |
| 171 | // special case: [first,prev] have exact Names |
| 172 | if (prev->getName().size() == interestNameLength) { |
| 173 | NFD_LOG_TRACE(" find-among-exact " << prev->getName()); |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 174 | iterator matchExact = this->findRightmostAmongExact(interest, first, right); |
Junxiao Shi | 5640ec8 | 2015-01-07 21:51:19 -0700 | [diff] [blame] | 175 | return matchExact == right ? last : matchExact; |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 176 | } |
| 177 | |
Junxiao Shi | 5640ec8 | 2015-01-07 21:51:19 -0700 | [diff] [blame] | 178 | Name prefix = prev->getName().getPrefix(interestNameLength + 1); |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 179 | iterator left = m_table.lower_bound(prefix); |
Junxiao Shi | 5640ec8 | 2015-01-07 21:51:19 -0700 | [diff] [blame] | 180 | |
| 181 | // normal case: [left,right) are under one-component-longer prefix |
| 182 | NFD_LOG_TRACE(" find-under-prefix " << prefix); |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 183 | iterator match = this->findLeftmost(interest, left, right); |
Junxiao Shi | 5640ec8 | 2015-01-07 21:51:19 -0700 | [diff] [blame] | 184 | if (match != right) { |
| 185 | return match; |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 186 | } |
Junxiao Shi | 5640ec8 | 2015-01-07 21:51:19 -0700 | [diff] [blame] | 187 | right = left; |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 188 | } |
Junxiao Shi | 5640ec8 | 2015-01-07 21:51:19 -0700 | [diff] [blame] | 189 | return last; |
| 190 | } |
| 191 | |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 192 | iterator |
| 193 | Cs::findRightmostAmongExact(const Interest& interest, iterator first, iterator last) const |
Junxiao Shi | 5640ec8 | 2015-01-07 21:51:19 -0700 | [diff] [blame] | 194 | { |
Davide Pesavento | e4b2238 | 2018-06-10 14:37:24 -0400 | [diff] [blame] | 195 | return find_last_if(first, last, [&interest] (const auto& entry) { return entry.canSatisfy(interest); }); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | void |
Junxiao Shi | 3d2049f | 2018-01-26 23:46:06 +0000 | [diff] [blame] | 199 | Cs::dump() |
| 200 | { |
| 201 | NFD_LOG_DEBUG("dump table"); |
| 202 | for (const EntryImpl& entry : m_table) { |
| 203 | NFD_LOG_TRACE(entry.getFullName()); |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | void |
| 208 | Cs::setPolicy(unique_ptr<Policy> policy) |
| 209 | { |
| 210 | BOOST_ASSERT(policy != nullptr); |
| 211 | BOOST_ASSERT(m_policy != nullptr); |
| 212 | size_t limit = m_policy->getLimit(); |
| 213 | this->setPolicyImpl(std::move(policy)); |
| 214 | m_policy->setLimit(limit); |
| 215 | } |
| 216 | |
| 217 | void |
Junxiao Shi | 52555b0 | 2016-11-25 21:39:06 +0000 | [diff] [blame] | 218 | Cs::setPolicyImpl(unique_ptr<Policy> policy) |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 219 | { |
Junxiao Shi | 52555b0 | 2016-11-25 21:39:06 +0000 | [diff] [blame] | 220 | NFD_LOG_DEBUG("set-policy " << policy->getName()); |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 221 | m_policy = std::move(policy); |
| 222 | m_beforeEvictConnection = m_policy->beforeEvict.connect([this] (iterator it) { |
| 223 | m_table.erase(it); |
| 224 | }); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 225 | |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 226 | m_policy->setCs(this); |
| 227 | BOOST_ASSERT(m_policy->getCs() == this); |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | void |
Junxiao Shi | 3d2049f | 2018-01-26 23:46:06 +0000 | [diff] [blame] | 231 | Cs::enableAdmit(bool shouldAdmit) |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 232 | { |
Junxiao Shi | 3d2049f | 2018-01-26 23:46:06 +0000 | [diff] [blame] | 233 | if (m_shouldAdmit == shouldAdmit) { |
| 234 | return; |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 235 | } |
Junxiao Shi | 3d2049f | 2018-01-26 23:46:06 +0000 | [diff] [blame] | 236 | m_shouldAdmit = shouldAdmit; |
| 237 | NFD_LOG_INFO((shouldAdmit ? "Enabling" : "Disabling") << " Data admittance"); |
| 238 | } |
| 239 | |
| 240 | void |
| 241 | Cs::enableServe(bool shouldServe) |
| 242 | { |
| 243 | if (m_shouldServe == shouldServe) { |
| 244 | return; |
| 245 | } |
| 246 | m_shouldServe = shouldServe; |
| 247 | NFD_LOG_INFO((shouldServe ? "Enabling" : "Disabling") << " Data serving"); |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | } // namespace cs |
| 251 | } // namespace nfd |