blob: 18d8fe2c6ec26200dac7b1051d73ad98d70bfa35 [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 Pesaventoa3a7a4e2022-05-29 16:06:22 -04003 * Copyright (c) 2014-2022, 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"
Davide Pesavento2cae8ca2019-04-18 20:48:05 -040027#include "common/logger.hpp"
Davide Pesavento981db802018-04-10 18:05:04 -040028
Junxiao Shicbc8e942016-09-06 03:17:45 +000029#include <ndn-cxx/lp/tags.hpp>
Davide Pesavento981db802018-04-10 18:05:04 -040030#include <ndn-cxx/util/concepts.hpp>
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080031
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040032namespace nfd::cs {
Junxiao Shi0fcb41e2014-01-24 10:29:43 -070033
Davide Pesaventoa3148082018-04-12 18:21:54 -040034NFD_LOG_INIT(ContentStore);
Junxiao Shifc206962015-01-16 11:12:22 -070035
Chavoosh Ghasemi32e76092018-09-10 14:51:33 -070036static unique_ptr<Policy>
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050037makeDefaultPolicy()
Junxiao Shi0fcb41e2014-01-24 10:29:43 -070038{
Chavoosh Ghasemi32e76092018-09-10 14:51:33 -070039 return Policy::create("lru");
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050040}
41
Junxiao Shib4a5acd2016-12-07 19:59:18 +000042Cs::Cs(size_t nMaxPackets)
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050043{
Davide Pesavento3dade002019-03-19 11:29:56 -060044 setPolicyImpl(makeDefaultPolicy());
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050045 m_policy->setLimit(nMaxPackets);
Junxiao Shi0fcb41e2014-01-24 10:29:43 -070046}
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080047
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080048void
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080049Cs::insert(const Data& data, bool isUnsolicited)
Junxiao Shi0fcb41e2014-01-24 10:29:43 -070050{
Junxiao Shi3d2049f2018-01-26 23:46:06 +000051 if (!m_shouldAdmit || m_policy->getLimit() == 0) {
Minsheng Zhangffe8bbb2016-03-10 13:40:37 -070052 return;
53 }
Junxiao Shi3d2049f2018-01-26 23:46:06 +000054 NFD_LOG_DEBUG("insert " << data.getName());
Minsheng Zhangffe8bbb2016-03-10 13:40:37 -070055
Junxiao Shi0de23a22015-12-03 20:07:02 +000056 // recognize CachePolicy
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040057 auto tag = data.getTag<lp::CachePolicyTag>();
Junxiao Shi0de23a22015-12-03 20:07:02 +000058 if (tag != nullptr) {
59 lp::CachePolicyType policy = tag->get().getPolicy();
60 if (policy == lp::CachePolicyType::NO_CACHE) {
Minsheng Zhangffe8bbb2016-03-10 13:40:37 -070061 return;
Junxiao Shi35b16b12015-02-16 22:14:57 -070062 }
63 }
64
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040065 auto [it, isNewEntry] = m_table.emplace(data.shared_from_this(), isUnsolicited);
66 auto& entry = const_cast<Entry&>(*it);
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080067
Junxiao Shi5e4a02f2019-07-15 11:59:18 +000068 entry.updateFreshUntil();
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050069
Junxiao Shia9388182014-12-13 23:16:09 -070070 if (!isNewEntry) { // existing entry
Junxiao Shia9388182014-12-13 23:16:09 -070071 // XXX This doesn't forbid unsolicited Data from refreshing a solicited entry.
72 if (entry.isUnsolicited() && !isUnsolicited) {
Junxiao Shi5e4a02f2019-07-15 11:59:18 +000073 entry.clearUnsolicited();
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080074 }
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050075 m_policy->afterRefresh(it);
Junxiao Shiaf6569a2014-06-14 00:01:34 -070076 }
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050077 else {
78 m_policy->afterInsert(it);
79 }
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080080}
81
Junxiao Shi07f2e2f2019-07-22 09:10:06 -060082std::pair<Cs::const_iterator, Cs::const_iterator>
Junxiao Shi14b39182019-04-29 19:19:18 +000083Cs::findPrefixRange(const Name& prefix) const
Junxiao Shi30c37ab2018-04-09 14:26:47 +000084{
Junxiao Shi07f2e2f2019-07-22 09:10:06 -060085 auto first = m_table.lower_bound(prefix);
86 auto last = m_table.end();
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040087 if (!prefix.empty()) {
Junxiao Shi30c37ab2018-04-09 14:26:47 +000088 last = m_table.lower_bound(prefix.getSuccessor());
89 }
Junxiao Shi14b39182019-04-29 19:19:18 +000090 return {first, last};
91}
92
93size_t
94Cs::eraseImpl(const Name& prefix, size_t limit)
95{
Junxiao Shi07f2e2f2019-07-22 09:10:06 -060096 const_iterator i, last;
Junxiao Shi14b39182019-04-29 19:19:18 +000097 std::tie(i, last) = findPrefixRange(prefix);
Junxiao Shi30c37ab2018-04-09 14:26:47 +000098
99 size_t nErased = 0;
Junxiao Shi14b39182019-04-29 19:19:18 +0000100 while (i != last && nErased < limit) {
101 m_policy->beforeErase(i);
102 i = m_table.erase(i);
Junxiao Shi30c37ab2018-04-09 14:26:47 +0000103 ++nErased;
104 }
Junxiao Shi14b39182019-04-29 19:19:18 +0000105 return nErased;
Junxiao Shi30c37ab2018-04-09 14:26:47 +0000106}
107
Junxiao Shi07f2e2f2019-07-22 09:10:06 -0600108Cs::const_iterator
Junxiao Shi14b39182019-04-29 19:19:18 +0000109Cs::findImpl(const Interest& interest) const
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800110{
Junxiao Shi3d2049f2018-01-26 23:46:06 +0000111 if (!m_shouldServe || m_policy->getLimit() == 0) {
Junxiao Shi14b39182019-04-29 19:19:18 +0000112 return m_table.end();
Junxiao Shi3d2049f2018-01-26 23:46:06 +0000113 }
Junxiao Shi14b39182019-04-29 19:19:18 +0000114
Junxiao Shia9388182014-12-13 23:16:09 -0700115 const Name& prefix = interest.getName();
Junxiao Shi14b39182019-04-29 19:19:18 +0000116 auto range = findPrefixRange(prefix);
117 auto match = std::find_if(range.first, range.second,
118 [&interest] (const auto& entry) { return entry.canSatisfy(interest); });
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800119
Junxiao Shi14b39182019-04-29 19:19:18 +0000120 if (match == range.second) {
121 NFD_LOG_DEBUG("find " << prefix << " no-match");
122 return m_table.end();
Junxiao Shia9388182014-12-13 23:16:09 -0700123 }
Junxiao Shi14b39182019-04-29 19:19:18 +0000124 NFD_LOG_DEBUG("find " << prefix << " matching " << match->getName());
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500125 m_policy->beforeUse(match);
Junxiao Shi14b39182019-04-29 19:19:18 +0000126 return match;
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800127}
128
129void
Junxiao Shi3d2049f2018-01-26 23:46:06 +0000130Cs::dump()
131{
132 NFD_LOG_DEBUG("dump table");
Junxiao Shi5e4a02f2019-07-15 11:59:18 +0000133 for (const Entry& entry : m_table) {
Junxiao Shi3d2049f2018-01-26 23:46:06 +0000134 NFD_LOG_TRACE(entry.getFullName());
135 }
136}
137
138void
139Cs::setPolicy(unique_ptr<Policy> policy)
140{
141 BOOST_ASSERT(policy != nullptr);
142 BOOST_ASSERT(m_policy != nullptr);
143 size_t limit = m_policy->getLimit();
144 this->setPolicyImpl(std::move(policy));
145 m_policy->setLimit(limit);
146}
147
148void
Junxiao Shi52555b02016-11-25 21:39:06 +0000149Cs::setPolicyImpl(unique_ptr<Policy> policy)
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800150{
Junxiao Shi52555b02016-11-25 21:39:06 +0000151 NFD_LOG_DEBUG("set-policy " << policy->getName());
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500152 m_policy = std::move(policy);
Junxiao Shi07f2e2f2019-07-22 09:10:06 -0600153 m_beforeEvictConnection = m_policy->beforeEvict.connect([this] (auto it) { m_table.erase(it); });
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800154
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500155 m_policy->setCs(this);
156 BOOST_ASSERT(m_policy->getCs() == this);
Junxiao Shia9388182014-12-13 23:16:09 -0700157}
158
159void
Junxiao Shi3d2049f2018-01-26 23:46:06 +0000160Cs::enableAdmit(bool shouldAdmit)
Junxiao Shia9388182014-12-13 23:16:09 -0700161{
Junxiao Shi3d2049f2018-01-26 23:46:06 +0000162 if (m_shouldAdmit == shouldAdmit) {
163 return;
Junxiao Shia9388182014-12-13 23:16:09 -0700164 }
Junxiao Shi3d2049f2018-01-26 23:46:06 +0000165 m_shouldAdmit = shouldAdmit;
166 NFD_LOG_INFO((shouldAdmit ? "Enabling" : "Disabling") << " Data admittance");
167}
168
169void
170Cs::enableServe(bool shouldServe)
171{
172 if (m_shouldServe == shouldServe) {
173 return;
174 }
175 m_shouldServe = shouldServe;
176 NFD_LOG_INFO((shouldServe ? "Enabling" : "Disabling") << " Data serving");
Junxiao Shia9388182014-12-13 23:16:09 -0700177}
178
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400179} // namespace nfd::cs