blob: 5fc83019cda362a0a3ab4d6f868a1054ed64141b [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"
Junxiao Shi5640ec82015-01-07 21:51:19 -070028#include "core/algorithm.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 Pesaventoa3148082018-04-12 18:21:54 -040036NFD_LOG_INIT(ContentStore);
Junxiao Shifc206962015-01-16 11:12:22 -070037
Chavoosh Ghasemi32e76092018-09-10 14:51:33 -070038static unique_ptr<Policy>
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050039makeDefaultPolicy()
Junxiao Shi0fcb41e2014-01-24 10:29:43 -070040{
Chavoosh Ghasemi32e76092018-09-10 14:51:33 -070041 return Policy::create("lru");
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050042}
43
Junxiao Shib4a5acd2016-12-07 19:59:18 +000044Cs::Cs(size_t nMaxPackets)
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050045{
Davide Pesavento3dade002019-03-19 11:29:56 -060046 setPolicyImpl(makeDefaultPolicy());
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050047 m_policy->setLimit(nMaxPackets);
Junxiao Shi0fcb41e2014-01-24 10:29:43 -070048}
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080049
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080050void
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080051Cs::insert(const Data& data, bool isUnsolicited)
Junxiao Shi0fcb41e2014-01-24 10:29:43 -070052{
Junxiao Shi3d2049f2018-01-26 23:46:06 +000053 if (!m_shouldAdmit || m_policy->getLimit() == 0) {
Minsheng Zhangffe8bbb2016-03-10 13:40:37 -070054 return;
55 }
Junxiao Shi3d2049f2018-01-26 23:46:06 +000056 NFD_LOG_DEBUG("insert " << data.getName());
Minsheng Zhangffe8bbb2016-03-10 13:40:37 -070057
Junxiao Shi0de23a22015-12-03 20:07:02 +000058 // recognize CachePolicy
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040059 auto tag = data.getTag<lp::CachePolicyTag>();
Junxiao Shi0de23a22015-12-03 20:07:02 +000060 if (tag != nullptr) {
61 lp::CachePolicyType policy = tag->get().getPolicy();
62 if (policy == lp::CachePolicyType::NO_CACHE) {
Minsheng Zhangffe8bbb2016-03-10 13:40:37 -070063 return;
Junxiao Shi35b16b12015-02-16 22:14:57 -070064 }
65 }
66
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040067 auto [it, isNewEntry] = m_table.emplace(data.shared_from_this(), isUnsolicited);
68 auto& entry = const_cast<Entry&>(*it);
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080069
Junxiao Shi5e4a02f2019-07-15 11:59:18 +000070 entry.updateFreshUntil();
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050071
Junxiao Shia9388182014-12-13 23:16:09 -070072 if (!isNewEntry) { // existing entry
Junxiao Shia9388182014-12-13 23:16:09 -070073 // XXX This doesn't forbid unsolicited Data from refreshing a solicited entry.
74 if (entry.isUnsolicited() && !isUnsolicited) {
Junxiao Shi5e4a02f2019-07-15 11:59:18 +000075 entry.clearUnsolicited();
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080076 }
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050077 m_policy->afterRefresh(it);
Junxiao Shiaf6569a2014-06-14 00:01:34 -070078 }
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050079 else {
80 m_policy->afterInsert(it);
81 }
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080082}
83
Junxiao Shi07f2e2f2019-07-22 09:10:06 -060084std::pair<Cs::const_iterator, Cs::const_iterator>
Junxiao Shi14b39182019-04-29 19:19:18 +000085Cs::findPrefixRange(const Name& prefix) const
Junxiao Shi30c37ab2018-04-09 14:26:47 +000086{
Junxiao Shi07f2e2f2019-07-22 09:10:06 -060087 auto first = m_table.lower_bound(prefix);
88 auto last = m_table.end();
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040089 if (!prefix.empty()) {
Junxiao Shi30c37ab2018-04-09 14:26:47 +000090 last = m_table.lower_bound(prefix.getSuccessor());
91 }
Junxiao Shi14b39182019-04-29 19:19:18 +000092 return {first, last};
93}
94
95size_t
96Cs::eraseImpl(const Name& prefix, size_t limit)
97{
Junxiao Shi07f2e2f2019-07-22 09:10:06 -060098 const_iterator i, last;
Junxiao Shi14b39182019-04-29 19:19:18 +000099 std::tie(i, last) = findPrefixRange(prefix);
Junxiao Shi30c37ab2018-04-09 14:26:47 +0000100
101 size_t nErased = 0;
Junxiao Shi14b39182019-04-29 19:19:18 +0000102 while (i != last && nErased < limit) {
103 m_policy->beforeErase(i);
104 i = m_table.erase(i);
Junxiao Shi30c37ab2018-04-09 14:26:47 +0000105 ++nErased;
106 }
Junxiao Shi14b39182019-04-29 19:19:18 +0000107 return nErased;
Junxiao Shi30c37ab2018-04-09 14:26:47 +0000108}
109
Junxiao Shi07f2e2f2019-07-22 09:10:06 -0600110Cs::const_iterator
Junxiao Shi14b39182019-04-29 19:19:18 +0000111Cs::findImpl(const Interest& interest) const
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800112{
Junxiao Shi3d2049f2018-01-26 23:46:06 +0000113 if (!m_shouldServe || m_policy->getLimit() == 0) {
Junxiao Shi14b39182019-04-29 19:19:18 +0000114 return m_table.end();
Junxiao Shi3d2049f2018-01-26 23:46:06 +0000115 }
Junxiao Shi14b39182019-04-29 19:19:18 +0000116
Junxiao Shia9388182014-12-13 23:16:09 -0700117 const Name& prefix = interest.getName();
Junxiao Shi14b39182019-04-29 19:19:18 +0000118 auto range = findPrefixRange(prefix);
119 auto match = std::find_if(range.first, range.second,
120 [&interest] (const auto& entry) { return entry.canSatisfy(interest); });
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800121
Junxiao Shi14b39182019-04-29 19:19:18 +0000122 if (match == range.second) {
123 NFD_LOG_DEBUG("find " << prefix << " no-match");
124 return m_table.end();
Junxiao Shia9388182014-12-13 23:16:09 -0700125 }
Junxiao Shi14b39182019-04-29 19:19:18 +0000126 NFD_LOG_DEBUG("find " << prefix << " matching " << match->getName());
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500127 m_policy->beforeUse(match);
Junxiao Shi14b39182019-04-29 19:19:18 +0000128 return match;
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800129}
130
131void
Junxiao Shi3d2049f2018-01-26 23:46:06 +0000132Cs::dump()
133{
134 NFD_LOG_DEBUG("dump table");
Junxiao Shi5e4a02f2019-07-15 11:59:18 +0000135 for (const Entry& entry : m_table) {
Junxiao Shi3d2049f2018-01-26 23:46:06 +0000136 NFD_LOG_TRACE(entry.getFullName());
137 }
138}
139
140void
141Cs::setPolicy(unique_ptr<Policy> policy)
142{
143 BOOST_ASSERT(policy != nullptr);
144 BOOST_ASSERT(m_policy != nullptr);
145 size_t limit = m_policy->getLimit();
146 this->setPolicyImpl(std::move(policy));
147 m_policy->setLimit(limit);
148}
149
150void
Junxiao Shi52555b02016-11-25 21:39:06 +0000151Cs::setPolicyImpl(unique_ptr<Policy> policy)
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800152{
Junxiao Shi52555b02016-11-25 21:39:06 +0000153 NFD_LOG_DEBUG("set-policy " << policy->getName());
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500154 m_policy = std::move(policy);
Junxiao Shi07f2e2f2019-07-22 09:10:06 -0600155 m_beforeEvictConnection = m_policy->beforeEvict.connect([this] (auto it) { m_table.erase(it); });
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800156
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500157 m_policy->setCs(this);
158 BOOST_ASSERT(m_policy->getCs() == this);
Junxiao Shia9388182014-12-13 23:16:09 -0700159}
160
161void
Junxiao Shi3d2049f2018-01-26 23:46:06 +0000162Cs::enableAdmit(bool shouldAdmit)
Junxiao Shia9388182014-12-13 23:16:09 -0700163{
Junxiao Shi3d2049f2018-01-26 23:46:06 +0000164 if (m_shouldAdmit == shouldAdmit) {
165 return;
Junxiao Shia9388182014-12-13 23:16:09 -0700166 }
Junxiao Shi3d2049f2018-01-26 23:46:06 +0000167 m_shouldAdmit = shouldAdmit;
168 NFD_LOG_INFO((shouldAdmit ? "Enabling" : "Disabling") << " Data admittance");
169}
170
171void
172Cs::enableServe(bool shouldServe)
173{
174 if (m_shouldServe == shouldServe) {
175 return;
176 }
177 m_shouldServe = shouldServe;
178 NFD_LOG_INFO((shouldServe ? "Enabling" : "Disabling") << " Data serving");
Junxiao Shia9388182014-12-13 23:16:09 -0700179}
180
181} // namespace cs
182} // namespace nfd