blob: 00dc9ee52b02f3e3ee643743313c24c7f767937e [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
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050040unique_ptr<Policy>
41makeDefaultPolicy()
Junxiao Shi0fcb41e2014-01-24 10:29:43 -070042{
Junxiao Shi52555b02016-11-25 21:39:06 +000043 const std::string DEFAULT_POLICY = "priority_fifo";
44 return Policy::create(DEFAULT_POLICY);
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050045}
46
Junxiao Shib4a5acd2016-12-07 19:59:18 +000047Cs::Cs(size_t nMaxPackets)
Junxiao Shi3d2049f2018-01-26 23:46:06 +000048 : m_shouldAdmit(true)
49 , m_shouldServe(true)
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050050{
Junxiao Shib4a5acd2016-12-07 19:59:18 +000051 this->setPolicyImpl(makeDefaultPolicy());
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050052 m_policy->setLimit(nMaxPackets);
Junxiao Shi0fcb41e2014-01-24 10:29:43 -070053}
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080054
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080055void
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080056Cs::insert(const Data& data, bool isUnsolicited)
Junxiao Shi0fcb41e2014-01-24 10:29:43 -070057{
Junxiao Shi3d2049f2018-01-26 23:46:06 +000058 if (!m_shouldAdmit || m_policy->getLimit() == 0) {
Minsheng Zhangffe8bbb2016-03-10 13:40:37 -070059 return;
60 }
Junxiao Shi3d2049f2018-01-26 23:46:06 +000061 NFD_LOG_DEBUG("insert " << data.getName());
Minsheng Zhangffe8bbb2016-03-10 13:40:37 -070062
Junxiao Shi0de23a22015-12-03 20:07:02 +000063 // recognize CachePolicy
64 shared_ptr<lp::CachePolicyTag> tag = data.getTag<lp::CachePolicyTag>();
65 if (tag != nullptr) {
66 lp::CachePolicyType policy = tag->get().getPolicy();
67 if (policy == lp::CachePolicyType::NO_CACHE) {
Minsheng Zhangffe8bbb2016-03-10 13:40:37 -070068 return;
Junxiao Shi35b16b12015-02-16 22:14:57 -070069 }
70 }
71
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050072 iterator it;
Junxiao Shi3d2049f2018-01-26 23:46:06 +000073 bool isNewEntry = false;
74 std::tie(it, isNewEntry) = m_table.emplace(data.shared_from_this(), isUnsolicited);
Junxiao Shifc206962015-01-16 11:12:22 -070075 EntryImpl& entry = const_cast<EntryImpl&>(*it);
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080076
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050077 entry.updateStaleTime();
78
Junxiao Shia9388182014-12-13 23:16:09 -070079 if (!isNewEntry) { // existing entry
Junxiao Shia9388182014-12-13 23:16:09 -070080 // XXX This doesn't forbid unsolicited Data from refreshing a solicited entry.
81 if (entry.isUnsolicited() && !isUnsolicited) {
82 entry.unsetUnsolicited();
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080083 }
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050084
85 m_policy->afterRefresh(it);
Junxiao Shiaf6569a2014-06-14 00:01:34 -070086 }
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050087 else {
88 m_policy->afterInsert(it);
89 }
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080090}
91
mzhang4eab72492015-02-25 11:16:09 -060092void
Junxiao Shi30c37ab2018-04-09 14:26:47 +000093Cs::erase(const Name& prefix, size_t limit, const AfterEraseCallback& cb)
94{
95 BOOST_ASSERT(static_cast<bool>(cb));
96
97 iterator first = m_table.lower_bound(prefix);
98 iterator last = m_table.end();
99 if (prefix.size() > 0) {
100 last = m_table.lower_bound(prefix.getSuccessor());
101 }
102
103 size_t nErased = 0;
104 while (first != last && nErased < limit) {
105 m_policy->beforeErase(first);
106 first = m_table.erase(first);
107 ++nErased;
108 }
109
110 if (cb) {
111 cb(nErased);
112 }
113}
114
115void
mzhang4eab72492015-02-25 11:16:09 -0600116Cs::find(const Interest& interest,
117 const HitCallback& hitCallback,
118 const MissCallback& missCallback) const
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800119{
mzhang4eab72492015-02-25 11:16:09 -0600120 BOOST_ASSERT(static_cast<bool>(hitCallback));
121 BOOST_ASSERT(static_cast<bool>(missCallback));
122
Junxiao Shi3d2049f2018-01-26 23:46:06 +0000123 if (!m_shouldServe || m_policy->getLimit() == 0) {
124 missCallback(interest);
125 return;
126 }
Junxiao Shia9388182014-12-13 23:16:09 -0700127 const Name& prefix = interest.getName();
128 bool isRightmost = interest.getChildSelector() == 1;
129 NFD_LOG_DEBUG("find " << prefix << (isRightmost ? " R" : " L"));
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800130
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500131 iterator first = m_table.lower_bound(prefix);
132 iterator last = m_table.end();
Junxiao Shia9388182014-12-13 23:16:09 -0700133 if (prefix.size() > 0) {
Junxiao Shi5640ec82015-01-07 21:51:19 -0700134 last = m_table.lower_bound(prefix.getSuccessor());
Junxiao Shia9388182014-12-13 23:16:09 -0700135 }
Junxiao Shi5640ec82015-01-07 21:51:19 -0700136
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500137 iterator match = last;
Junxiao Shi5640ec82015-01-07 21:51:19 -0700138 if (isRightmost) {
139 match = this->findRightmost(interest, first, last);
140 }
141 else {
142 match = this->findLeftmost(interest, first, last);
143 }
144
145 if (match == last) {
146 NFD_LOG_DEBUG(" no-match");
mzhang4eab72492015-02-25 11:16:09 -0600147 missCallback(interest);
148 return;
Junxiao Shi5640ec82015-01-07 21:51:19 -0700149 }
150 NFD_LOG_DEBUG(" matching " << match->getName());
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500151 m_policy->beforeUse(match);
mzhang4eab72492015-02-25 11:16:09 -0600152 hitCallback(interest, match->getData());
Junxiao Shi5640ec82015-01-07 21:51:19 -0700153}
154
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500155iterator
156Cs::findLeftmost(const Interest& interest, iterator first, iterator last) const
Junxiao Shi5640ec82015-01-07 21:51:19 -0700157{
Junxiao Shifc206962015-01-16 11:12:22 -0700158 return std::find_if(first, last, bind(&cs::EntryImpl::canSatisfy, _1, interest));
Junxiao Shi5640ec82015-01-07 21:51:19 -0700159}
160
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500161iterator
162Cs::findRightmost(const Interest& interest, iterator first, iterator last) const
Junxiao Shi5640ec82015-01-07 21:51:19 -0700163{
164 // Each loop visits a sub-namespace under a prefix one component longer than Interest Name.
165 // If there is a match in that sub-namespace, the leftmost match is returned;
166 // otherwise, loop continues.
167
168 size_t interestNameLength = interest.getName().size();
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500169 for (iterator right = last; right != first;) {
170 iterator prev = std::prev(right);
Junxiao Shi5640ec82015-01-07 21:51:19 -0700171
172 // special case: [first,prev] have exact Names
173 if (prev->getName().size() == interestNameLength) {
174 NFD_LOG_TRACE(" find-among-exact " << prev->getName());
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500175 iterator matchExact = this->findRightmostAmongExact(interest, first, right);
Junxiao Shi5640ec82015-01-07 21:51:19 -0700176 return matchExact == right ? last : matchExact;
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800177 }
178
Junxiao Shi5640ec82015-01-07 21:51:19 -0700179 Name prefix = prev->getName().getPrefix(interestNameLength + 1);
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500180 iterator left = m_table.lower_bound(prefix);
Junxiao Shi5640ec82015-01-07 21:51:19 -0700181
182 // normal case: [left,right) are under one-component-longer prefix
183 NFD_LOG_TRACE(" find-under-prefix " << prefix);
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500184 iterator match = this->findLeftmost(interest, left, right);
Junxiao Shi5640ec82015-01-07 21:51:19 -0700185 if (match != right) {
186 return match;
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800187 }
Junxiao Shi5640ec82015-01-07 21:51:19 -0700188 right = left;
Junxiao Shia9388182014-12-13 23:16:09 -0700189 }
Junxiao Shi5640ec82015-01-07 21:51:19 -0700190 return last;
191}
192
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500193iterator
194Cs::findRightmostAmongExact(const Interest& interest, iterator first, iterator last) const
Junxiao Shi5640ec82015-01-07 21:51:19 -0700195{
Junxiao Shifc206962015-01-16 11:12:22 -0700196 return find_last_if(first, last, bind(&EntryImpl::canSatisfy, _1, interest));
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800197}
198
199void
Junxiao Shi3d2049f2018-01-26 23:46:06 +0000200Cs::dump()
201{
202 NFD_LOG_DEBUG("dump table");
203 for (const EntryImpl& entry : m_table) {
204 NFD_LOG_TRACE(entry.getFullName());
205 }
206}
207
208void
209Cs::setPolicy(unique_ptr<Policy> policy)
210{
211 BOOST_ASSERT(policy != nullptr);
212 BOOST_ASSERT(m_policy != nullptr);
213 size_t limit = m_policy->getLimit();
214 this->setPolicyImpl(std::move(policy));
215 m_policy->setLimit(limit);
216}
217
218void
Junxiao Shi52555b02016-11-25 21:39:06 +0000219Cs::setPolicyImpl(unique_ptr<Policy> policy)
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800220{
Junxiao Shi52555b02016-11-25 21:39:06 +0000221 NFD_LOG_DEBUG("set-policy " << policy->getName());
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500222 m_policy = std::move(policy);
223 m_beforeEvictConnection = m_policy->beforeEvict.connect([this] (iterator it) {
224 m_table.erase(it);
225 });
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800226
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500227 m_policy->setCs(this);
228 BOOST_ASSERT(m_policy->getCs() == this);
Junxiao Shia9388182014-12-13 23:16:09 -0700229}
230
231void
Junxiao Shi3d2049f2018-01-26 23:46:06 +0000232Cs::enableAdmit(bool shouldAdmit)
Junxiao Shia9388182014-12-13 23:16:09 -0700233{
Junxiao Shi3d2049f2018-01-26 23:46:06 +0000234 if (m_shouldAdmit == shouldAdmit) {
235 return;
Junxiao Shia9388182014-12-13 23:16:09 -0700236 }
Junxiao Shi3d2049f2018-01-26 23:46:06 +0000237 m_shouldAdmit = shouldAdmit;
238 NFD_LOG_INFO((shouldAdmit ? "Enabling" : "Disabling") << " Data admittance");
239}
240
241void
242Cs::enableServe(bool shouldServe)
243{
244 if (m_shouldServe == shouldServe) {
245 return;
246 }
247 m_shouldServe = shouldServe;
248 NFD_LOG_INFO((shouldServe ? "Enabling" : "Disabling") << " Data serving");
Junxiao Shia9388182014-12-13 23:16:09 -0700249}
250
251} // namespace cs
252} // namespace nfd