blob: a9583740a86c56a1738985cc6876f76205384d8a [file] [log] [blame]
Junxiao Shi0fcb41e2014-01-24 10:29:43 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shi3d2049f2018-01-26 23:46:06 +00002/*
Davide Pesavento3dade002019-03-19 11:29:56 -06003 * Copyright (c) 2014-2019, Regents of the University of California,
Junxiao Shi5640ec82015-01-07 21:51:19 -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.
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080010 *
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070011 * 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 Shi0fcb41e2014-01-24 10:29:43 -070024 */
25
Junxiao Shi0fcb41e2014-01-24 10:29:43 -070026#include "cs.hpp"
Junxiao Shi5640ec82015-01-07 21:51:19 -070027#include "core/algorithm.hpp"
Junxiao Shi3aba7542016-12-24 02:42:52 +000028#include "core/logger.hpp"
Davide Pesavento981db802018-04-10 18:05:04 -040029
Junxiao Shicbc8e942016-09-06 03:17:45 +000030#include <ndn-cxx/lp/tags.hpp>
Davide Pesavento981db802018-04-10 18:05:04 -040031#include <ndn-cxx/util/concepts.hpp>
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080032
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080033namespace nfd {
Junxiao Shia9388182014-12-13 23:16:09 -070034namespace cs {
Junxiao Shi0fcb41e2014-01-24 10:29:43 -070035
Davide Pesavento981db802018-04-10 18:05:04 -040036NDN_CXX_ASSERT_FORWARD_ITERATOR(Cs::const_iterator);
Junxiao Shi3aba7542016-12-24 02:42:52 +000037
Davide Pesaventoa3148082018-04-12 18:21:54 -040038NFD_LOG_INIT(ContentStore);
Junxiao Shifc206962015-01-16 11:12:22 -070039
Chavoosh Ghasemi32e76092018-09-10 14:51:33 -070040static unique_ptr<Policy>
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050041makeDefaultPolicy()
Junxiao Shi0fcb41e2014-01-24 10:29:43 -070042{
Chavoosh Ghasemi32e76092018-09-10 14:51:33 -070043 return Policy::create("lru");
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050044}
45
Junxiao Shib4a5acd2016-12-07 19:59:18 +000046Cs::Cs(size_t nMaxPackets)
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050047{
Davide Pesavento3dade002019-03-19 11:29:56 -060048 setPolicyImpl(makeDefaultPolicy());
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050049 m_policy->setLimit(nMaxPackets);
Junxiao Shi0fcb41e2014-01-24 10:29:43 -070050}
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080051
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080052void
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080053Cs::insert(const Data& data, bool isUnsolicited)
Junxiao Shi0fcb41e2014-01-24 10:29:43 -070054{
Junxiao Shi3d2049f2018-01-26 23:46:06 +000055 if (!m_shouldAdmit || m_policy->getLimit() == 0) {
Minsheng Zhangffe8bbb2016-03-10 13:40:37 -070056 return;
57 }
Junxiao Shi3d2049f2018-01-26 23:46:06 +000058 NFD_LOG_DEBUG("insert " << data.getName());
Minsheng Zhangffe8bbb2016-03-10 13:40:37 -070059
Junxiao Shi0de23a22015-12-03 20:07:02 +000060 // recognize CachePolicy
61 shared_ptr<lp::CachePolicyTag> tag = data.getTag<lp::CachePolicyTag>();
62 if (tag != nullptr) {
63 lp::CachePolicyType policy = tag->get().getPolicy();
64 if (policy == lp::CachePolicyType::NO_CACHE) {
Minsheng Zhangffe8bbb2016-03-10 13:40:37 -070065 return;
Junxiao Shi35b16b12015-02-16 22:14:57 -070066 }
67 }
68
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050069 iterator it;
Junxiao Shi3d2049f2018-01-26 23:46:06 +000070 bool isNewEntry = false;
71 std::tie(it, isNewEntry) = m_table.emplace(data.shared_from_this(), isUnsolicited);
Junxiao Shifc206962015-01-16 11:12:22 -070072 EntryImpl& entry = const_cast<EntryImpl&>(*it);
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080073
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050074 entry.updateStaleTime();
75
Junxiao Shia9388182014-12-13 23:16:09 -070076 if (!isNewEntry) { // existing entry
Junxiao Shia9388182014-12-13 23:16:09 -070077 // XXX This doesn't forbid unsolicited Data from refreshing a solicited entry.
78 if (entry.isUnsolicited() && !isUnsolicited) {
79 entry.unsetUnsolicited();
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080080 }
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050081
82 m_policy->afterRefresh(it);
Junxiao Shiaf6569a2014-06-14 00:01:34 -070083 }
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050084 else {
85 m_policy->afterInsert(it);
86 }
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080087}
88
mzhang4eab72492015-02-25 11:16:09 -060089void
Junxiao Shi30c37ab2018-04-09 14:26:47 +000090Cs::erase(const Name& prefix, size_t limit, const AfterEraseCallback& cb)
91{
92 BOOST_ASSERT(static_cast<bool>(cb));
93
94 iterator first = m_table.lower_bound(prefix);
95 iterator last = m_table.end();
96 if (prefix.size() > 0) {
97 last = m_table.lower_bound(prefix.getSuccessor());
98 }
99
100 size_t nErased = 0;
101 while (first != last && nErased < limit) {
102 m_policy->beforeErase(first);
103 first = m_table.erase(first);
104 ++nErased;
105 }
106
107 if (cb) {
108 cb(nErased);
109 }
110}
111
112void
mzhang4eab72492015-02-25 11:16:09 -0600113Cs::find(const Interest& interest,
114 const HitCallback& hitCallback,
115 const MissCallback& missCallback) const
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800116{
mzhang4eab72492015-02-25 11:16:09 -0600117 BOOST_ASSERT(static_cast<bool>(hitCallback));
118 BOOST_ASSERT(static_cast<bool>(missCallback));
119
Junxiao Shi3d2049f2018-01-26 23:46:06 +0000120 if (!m_shouldServe || m_policy->getLimit() == 0) {
121 missCallback(interest);
122 return;
123 }
Junxiao Shia9388182014-12-13 23:16:09 -0700124 const Name& prefix = interest.getName();
125 bool isRightmost = interest.getChildSelector() == 1;
126 NFD_LOG_DEBUG("find " << prefix << (isRightmost ? " R" : " L"));
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800127
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500128 iterator first = m_table.lower_bound(prefix);
129 iterator last = m_table.end();
Junxiao Shia9388182014-12-13 23:16:09 -0700130 if (prefix.size() > 0) {
Junxiao Shi5640ec82015-01-07 21:51:19 -0700131 last = m_table.lower_bound(prefix.getSuccessor());
Junxiao Shia9388182014-12-13 23:16:09 -0700132 }
Junxiao Shi5640ec82015-01-07 21:51:19 -0700133
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500134 iterator match = last;
Junxiao Shi5640ec82015-01-07 21:51:19 -0700135 if (isRightmost) {
136 match = this->findRightmost(interest, first, last);
137 }
138 else {
139 match = this->findLeftmost(interest, first, last);
140 }
141
142 if (match == last) {
143 NFD_LOG_DEBUG(" no-match");
mzhang4eab72492015-02-25 11:16:09 -0600144 missCallback(interest);
145 return;
Junxiao Shi5640ec82015-01-07 21:51:19 -0700146 }
147 NFD_LOG_DEBUG(" matching " << match->getName());
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500148 m_policy->beforeUse(match);
mzhang4eab72492015-02-25 11:16:09 -0600149 hitCallback(interest, match->getData());
Junxiao Shi5640ec82015-01-07 21:51:19 -0700150}
151
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500152iterator
153Cs::findLeftmost(const Interest& interest, iterator first, iterator last) const
Junxiao Shi5640ec82015-01-07 21:51:19 -0700154{
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400155 return std::find_if(first, last, [&interest] (const auto& entry) { return entry.canSatisfy(interest); });
Junxiao Shi5640ec82015-01-07 21:51:19 -0700156}
157
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500158iterator
159Cs::findRightmost(const Interest& interest, iterator first, iterator last) const
Junxiao Shi5640ec82015-01-07 21:51:19 -0700160{
161 // Each loop visits a sub-namespace under a prefix one component longer than Interest Name.
162 // If there is a match in that sub-namespace, the leftmost match is returned;
163 // otherwise, loop continues.
164
165 size_t interestNameLength = interest.getName().size();
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500166 for (iterator right = last; right != first;) {
167 iterator prev = std::prev(right);
Junxiao Shi5640ec82015-01-07 21:51:19 -0700168
169 // special case: [first,prev] have exact Names
170 if (prev->getName().size() == interestNameLength) {
171 NFD_LOG_TRACE(" find-among-exact " << prev->getName());
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500172 iterator matchExact = this->findRightmostAmongExact(interest, first, right);
Junxiao Shi5640ec82015-01-07 21:51:19 -0700173 return matchExact == right ? last : matchExact;
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800174 }
175
Junxiao Shi5640ec82015-01-07 21:51:19 -0700176 Name prefix = prev->getName().getPrefix(interestNameLength + 1);
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500177 iterator left = m_table.lower_bound(prefix);
Junxiao Shi5640ec82015-01-07 21:51:19 -0700178
179 // normal case: [left,right) are under one-component-longer prefix
180 NFD_LOG_TRACE(" find-under-prefix " << prefix);
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500181 iterator match = this->findLeftmost(interest, left, right);
Junxiao Shi5640ec82015-01-07 21:51:19 -0700182 if (match != right) {
183 return match;
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800184 }
Junxiao Shi5640ec82015-01-07 21:51:19 -0700185 right = left;
Junxiao Shia9388182014-12-13 23:16:09 -0700186 }
Junxiao Shi5640ec82015-01-07 21:51:19 -0700187 return last;
188}
189
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500190iterator
191Cs::findRightmostAmongExact(const Interest& interest, iterator first, iterator last) const
Junxiao Shi5640ec82015-01-07 21:51:19 -0700192{
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400193 return find_last_if(first, last, [&interest] (const auto& entry) { return entry.canSatisfy(interest); });
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800194}
195
196void
Junxiao Shi3d2049f2018-01-26 23:46:06 +0000197Cs::dump()
198{
199 NFD_LOG_DEBUG("dump table");
200 for (const EntryImpl& entry : m_table) {
201 NFD_LOG_TRACE(entry.getFullName());
202 }
203}
204
205void
206Cs::setPolicy(unique_ptr<Policy> policy)
207{
208 BOOST_ASSERT(policy != nullptr);
209 BOOST_ASSERT(m_policy != nullptr);
210 size_t limit = m_policy->getLimit();
211 this->setPolicyImpl(std::move(policy));
212 m_policy->setLimit(limit);
213}
214
215void
Junxiao Shi52555b02016-11-25 21:39:06 +0000216Cs::setPolicyImpl(unique_ptr<Policy> policy)
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800217{
Junxiao Shi52555b02016-11-25 21:39:06 +0000218 NFD_LOG_DEBUG("set-policy " << policy->getName());
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500219 m_policy = std::move(policy);
220 m_beforeEvictConnection = m_policy->beforeEvict.connect([this] (iterator it) {
221 m_table.erase(it);
222 });
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800223
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500224 m_policy->setCs(this);
225 BOOST_ASSERT(m_policy->getCs() == this);
Junxiao Shia9388182014-12-13 23:16:09 -0700226}
227
228void
Junxiao Shi3d2049f2018-01-26 23:46:06 +0000229Cs::enableAdmit(bool shouldAdmit)
Junxiao Shia9388182014-12-13 23:16:09 -0700230{
Junxiao Shi3d2049f2018-01-26 23:46:06 +0000231 if (m_shouldAdmit == shouldAdmit) {
232 return;
Junxiao Shia9388182014-12-13 23:16:09 -0700233 }
Junxiao Shi3d2049f2018-01-26 23:46:06 +0000234 m_shouldAdmit = shouldAdmit;
235 NFD_LOG_INFO((shouldAdmit ? "Enabling" : "Disabling") << " Data admittance");
236}
237
238void
239Cs::enableServe(bool shouldServe)
240{
241 if (m_shouldServe == shouldServe) {
242 return;
243 }
244 m_shouldServe = shouldServe;
245 NFD_LOG_INFO((shouldServe ? "Enabling" : "Disabling") << " Data serving");
Junxiao Shia9388182014-12-13 23:16:09 -0700246}
247
248} // namespace cs
249} // namespace nfd