blob: 85ef3885cd6c5766d010349231639d74a1d9ab5f [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/*
3 * Copyright (c) 2014-2018, 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)
Junxiao Shi3d2049f2018-01-26 23:46:06 +000047 : m_shouldAdmit(true)
48 , m_shouldServe(true)
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050049{
Junxiao Shib4a5acd2016-12-07 19:59:18 +000050 this->setPolicyImpl(makeDefaultPolicy());
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050051 m_policy->setLimit(nMaxPackets);
Junxiao Shi0fcb41e2014-01-24 10:29:43 -070052}
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080053
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080054void
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080055Cs::insert(const Data& data, bool isUnsolicited)
Junxiao Shi0fcb41e2014-01-24 10:29:43 -070056{
Junxiao Shi3d2049f2018-01-26 23:46:06 +000057 if (!m_shouldAdmit || m_policy->getLimit() == 0) {
Minsheng Zhangffe8bbb2016-03-10 13:40:37 -070058 return;
59 }
Junxiao Shi3d2049f2018-01-26 23:46:06 +000060 NFD_LOG_DEBUG("insert " << data.getName());
Minsheng Zhangffe8bbb2016-03-10 13:40:37 -070061
Junxiao Shi0de23a22015-12-03 20:07:02 +000062 // 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 Zhangffe8bbb2016-03-10 13:40:37 -070067 return;
Junxiao Shi35b16b12015-02-16 22:14:57 -070068 }
69 }
70
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050071 iterator it;
Junxiao Shi3d2049f2018-01-26 23:46:06 +000072 bool isNewEntry = false;
73 std::tie(it, isNewEntry) = m_table.emplace(data.shared_from_this(), isUnsolicited);
Junxiao Shifc206962015-01-16 11:12:22 -070074 EntryImpl& entry = const_cast<EntryImpl&>(*it);
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080075
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050076 entry.updateStaleTime();
77
Junxiao Shia9388182014-12-13 23:16:09 -070078 if (!isNewEntry) { // existing entry
Junxiao Shia9388182014-12-13 23:16:09 -070079 // XXX This doesn't forbid unsolicited Data from refreshing a solicited entry.
80 if (entry.isUnsolicited() && !isUnsolicited) {
81 entry.unsetUnsolicited();
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080082 }
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050083
84 m_policy->afterRefresh(it);
Junxiao Shiaf6569a2014-06-14 00:01:34 -070085 }
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050086 else {
87 m_policy->afterInsert(it);
88 }
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080089}
90
mzhang4eab72492015-02-25 11:16:09 -060091void
Junxiao Shi30c37ab2018-04-09 14:26:47 +000092Cs::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
114void
mzhang4eab72492015-02-25 11:16:09 -0600115Cs::find(const Interest& interest,
116 const HitCallback& hitCallback,
117 const MissCallback& missCallback) const
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800118{
mzhang4eab72492015-02-25 11:16:09 -0600119 BOOST_ASSERT(static_cast<bool>(hitCallback));
120 BOOST_ASSERT(static_cast<bool>(missCallback));
121
Junxiao Shi3d2049f2018-01-26 23:46:06 +0000122 if (!m_shouldServe || m_policy->getLimit() == 0) {
123 missCallback(interest);
124 return;
125 }
Junxiao Shia9388182014-12-13 23:16:09 -0700126 const Name& prefix = interest.getName();
127 bool isRightmost = interest.getChildSelector() == 1;
128 NFD_LOG_DEBUG("find " << prefix << (isRightmost ? " R" : " L"));
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800129
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500130 iterator first = m_table.lower_bound(prefix);
131 iterator last = m_table.end();
Junxiao Shia9388182014-12-13 23:16:09 -0700132 if (prefix.size() > 0) {
Junxiao Shi5640ec82015-01-07 21:51:19 -0700133 last = m_table.lower_bound(prefix.getSuccessor());
Junxiao Shia9388182014-12-13 23:16:09 -0700134 }
Junxiao Shi5640ec82015-01-07 21:51:19 -0700135
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500136 iterator match = last;
Junxiao Shi5640ec82015-01-07 21:51:19 -0700137 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");
mzhang4eab72492015-02-25 11:16:09 -0600146 missCallback(interest);
147 return;
Junxiao Shi5640ec82015-01-07 21:51:19 -0700148 }
149 NFD_LOG_DEBUG(" matching " << match->getName());
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500150 m_policy->beforeUse(match);
mzhang4eab72492015-02-25 11:16:09 -0600151 hitCallback(interest, match->getData());
Junxiao Shi5640ec82015-01-07 21:51:19 -0700152}
153
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500154iterator
155Cs::findLeftmost(const Interest& interest, iterator first, iterator last) const
Junxiao Shi5640ec82015-01-07 21:51:19 -0700156{
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400157 return std::find_if(first, last, [&interest] (const auto& entry) { return entry.canSatisfy(interest); });
Junxiao Shi5640ec82015-01-07 21:51:19 -0700158}
159
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500160iterator
161Cs::findRightmost(const Interest& interest, iterator first, iterator last) const
Junxiao Shi5640ec82015-01-07 21:51:19 -0700162{
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 Zhangcb6e05f2015-04-20 15:51:47 -0500168 for (iterator right = last; right != first;) {
169 iterator prev = std::prev(right);
Junxiao Shi5640ec82015-01-07 21:51:19 -0700170
171 // special case: [first,prev] have exact Names
172 if (prev->getName().size() == interestNameLength) {
173 NFD_LOG_TRACE(" find-among-exact " << prev->getName());
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500174 iterator matchExact = this->findRightmostAmongExact(interest, first, right);
Junxiao Shi5640ec82015-01-07 21:51:19 -0700175 return matchExact == right ? last : matchExact;
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800176 }
177
Junxiao Shi5640ec82015-01-07 21:51:19 -0700178 Name prefix = prev->getName().getPrefix(interestNameLength + 1);
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500179 iterator left = m_table.lower_bound(prefix);
Junxiao Shi5640ec82015-01-07 21:51:19 -0700180
181 // normal case: [left,right) are under one-component-longer prefix
182 NFD_LOG_TRACE(" find-under-prefix " << prefix);
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500183 iterator match = this->findLeftmost(interest, left, right);
Junxiao Shi5640ec82015-01-07 21:51:19 -0700184 if (match != right) {
185 return match;
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800186 }
Junxiao Shi5640ec82015-01-07 21:51:19 -0700187 right = left;
Junxiao Shia9388182014-12-13 23:16:09 -0700188 }
Junxiao Shi5640ec82015-01-07 21:51:19 -0700189 return last;
190}
191
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500192iterator
193Cs::findRightmostAmongExact(const Interest& interest, iterator first, iterator last) const
Junxiao Shi5640ec82015-01-07 21:51:19 -0700194{
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400195 return find_last_if(first, last, [&interest] (const auto& entry) { return entry.canSatisfy(interest); });
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800196}
197
198void
Junxiao Shi3d2049f2018-01-26 23:46:06 +0000199Cs::dump()
200{
201 NFD_LOG_DEBUG("dump table");
202 for (const EntryImpl& entry : m_table) {
203 NFD_LOG_TRACE(entry.getFullName());
204 }
205}
206
207void
208Cs::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
217void
Junxiao Shi52555b02016-11-25 21:39:06 +0000218Cs::setPolicyImpl(unique_ptr<Policy> policy)
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800219{
Junxiao Shi52555b02016-11-25 21:39:06 +0000220 NFD_LOG_DEBUG("set-policy " << policy->getName());
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500221 m_policy = std::move(policy);
222 m_beforeEvictConnection = m_policy->beforeEvict.connect([this] (iterator it) {
223 m_table.erase(it);
224 });
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800225
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500226 m_policy->setCs(this);
227 BOOST_ASSERT(m_policy->getCs() == this);
Junxiao Shia9388182014-12-13 23:16:09 -0700228}
229
230void
Junxiao Shi3d2049f2018-01-26 23:46:06 +0000231Cs::enableAdmit(bool shouldAdmit)
Junxiao Shia9388182014-12-13 23:16:09 -0700232{
Junxiao Shi3d2049f2018-01-26 23:46:06 +0000233 if (m_shouldAdmit == shouldAdmit) {
234 return;
Junxiao Shia9388182014-12-13 23:16:09 -0700235 }
Junxiao Shi3d2049f2018-01-26 23:46:06 +0000236 m_shouldAdmit = shouldAdmit;
237 NFD_LOG_INFO((shouldAdmit ? "Enabling" : "Disabling") << " Data admittance");
238}
239
240void
241Cs::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 Shia9388182014-12-13 23:16:09 -0700248}
249
250} // namespace cs
251} // namespace nfd