blob: b240e3de8bb32cdfeb83f88de11ac349199812cb [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"
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
59 shared_ptr<lp::CachePolicyTag> tag = data.getTag<lp::CachePolicyTag>();
60 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
Junxiao Shi07f2e2f2019-07-22 09:10:06 -060067 const_iterator it;
Junxiao Shi3d2049f2018-01-26 23:46:06 +000068 bool isNewEntry = false;
69 std::tie(it, isNewEntry) = m_table.emplace(data.shared_from_this(), isUnsolicited);
Junxiao Shi5e4a02f2019-07-15 11:59:18 +000070 Entry& entry = const_cast<Entry&>(*it);
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080071
Junxiao Shi5e4a02f2019-07-15 11:59:18 +000072 entry.updateFreshUntil();
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050073
Junxiao Shia9388182014-12-13 23:16:09 -070074 if (!isNewEntry) { // existing entry
Junxiao Shia9388182014-12-13 23:16:09 -070075 // XXX This doesn't forbid unsolicited Data from refreshing a solicited entry.
76 if (entry.isUnsolicited() && !isUnsolicited) {
Junxiao Shi5e4a02f2019-07-15 11:59:18 +000077 entry.clearUnsolicited();
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080078 }
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050079
80 m_policy->afterRefresh(it);
Junxiao Shiaf6569a2014-06-14 00:01:34 -070081 }
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050082 else {
83 m_policy->afterInsert(it);
84 }
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080085}
86
Junxiao Shi07f2e2f2019-07-22 09:10:06 -060087std::pair<Cs::const_iterator, Cs::const_iterator>
Junxiao Shi14b39182019-04-29 19:19:18 +000088Cs::findPrefixRange(const Name& prefix) const
Junxiao Shi30c37ab2018-04-09 14:26:47 +000089{
Junxiao Shi07f2e2f2019-07-22 09:10:06 -060090 auto first = m_table.lower_bound(prefix);
91 auto last = m_table.end();
Junxiao Shi30c37ab2018-04-09 14:26:47 +000092 if (prefix.size() > 0) {
93 last = m_table.lower_bound(prefix.getSuccessor());
94 }
Junxiao Shi14b39182019-04-29 19:19:18 +000095 return {first, last};
96}
97
98size_t
99Cs::eraseImpl(const Name& prefix, size_t limit)
100{
Junxiao Shi07f2e2f2019-07-22 09:10:06 -0600101 const_iterator i, last;
Junxiao Shi14b39182019-04-29 19:19:18 +0000102 std::tie(i, last) = findPrefixRange(prefix);
Junxiao Shi30c37ab2018-04-09 14:26:47 +0000103
104 size_t nErased = 0;
Junxiao Shi14b39182019-04-29 19:19:18 +0000105 while (i != last && nErased < limit) {
106 m_policy->beforeErase(i);
107 i = m_table.erase(i);
Junxiao Shi30c37ab2018-04-09 14:26:47 +0000108 ++nErased;
109 }
Junxiao Shi14b39182019-04-29 19:19:18 +0000110 return nErased;
Junxiao Shi30c37ab2018-04-09 14:26:47 +0000111}
112
Junxiao Shi07f2e2f2019-07-22 09:10:06 -0600113Cs::const_iterator
Junxiao Shi14b39182019-04-29 19:19:18 +0000114Cs::findImpl(const Interest& interest) const
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800115{
Junxiao Shi3d2049f2018-01-26 23:46:06 +0000116 if (!m_shouldServe || m_policy->getLimit() == 0) {
Junxiao Shi14b39182019-04-29 19:19:18 +0000117 return m_table.end();
Junxiao Shi3d2049f2018-01-26 23:46:06 +0000118 }
Junxiao Shi14b39182019-04-29 19:19:18 +0000119
Junxiao Shia9388182014-12-13 23:16:09 -0700120 const Name& prefix = interest.getName();
Junxiao Shi14b39182019-04-29 19:19:18 +0000121 auto range = findPrefixRange(prefix);
122 auto match = std::find_if(range.first, range.second,
123 [&interest] (const auto& entry) { return entry.canSatisfy(interest); });
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800124
Junxiao Shi14b39182019-04-29 19:19:18 +0000125 if (match == range.second) {
126 NFD_LOG_DEBUG("find " << prefix << " no-match");
127 return m_table.end();
Junxiao Shia9388182014-12-13 23:16:09 -0700128 }
Junxiao Shi14b39182019-04-29 19:19:18 +0000129 NFD_LOG_DEBUG("find " << prefix << " matching " << match->getName());
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500130 m_policy->beforeUse(match);
Junxiao Shi14b39182019-04-29 19:19:18 +0000131 return match;
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800132}
133
134void
Junxiao Shi3d2049f2018-01-26 23:46:06 +0000135Cs::dump()
136{
137 NFD_LOG_DEBUG("dump table");
Junxiao Shi5e4a02f2019-07-15 11:59:18 +0000138 for (const Entry& entry : m_table) {
Junxiao Shi3d2049f2018-01-26 23:46:06 +0000139 NFD_LOG_TRACE(entry.getFullName());
140 }
141}
142
143void
144Cs::setPolicy(unique_ptr<Policy> policy)
145{
146 BOOST_ASSERT(policy != nullptr);
147 BOOST_ASSERT(m_policy != nullptr);
148 size_t limit = m_policy->getLimit();
149 this->setPolicyImpl(std::move(policy));
150 m_policy->setLimit(limit);
151}
152
153void
Junxiao Shi52555b02016-11-25 21:39:06 +0000154Cs::setPolicyImpl(unique_ptr<Policy> policy)
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800155{
Junxiao Shi52555b02016-11-25 21:39:06 +0000156 NFD_LOG_DEBUG("set-policy " << policy->getName());
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500157 m_policy = std::move(policy);
Junxiao Shi07f2e2f2019-07-22 09:10:06 -0600158 m_beforeEvictConnection = m_policy->beforeEvict.connect([this] (auto it) { m_table.erase(it); });
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800159
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500160 m_policy->setCs(this);
161 BOOST_ASSERT(m_policy->getCs() == this);
Junxiao Shia9388182014-12-13 23:16:09 -0700162}
163
164void
Junxiao Shi3d2049f2018-01-26 23:46:06 +0000165Cs::enableAdmit(bool shouldAdmit)
Junxiao Shia9388182014-12-13 23:16:09 -0700166{
Junxiao Shi3d2049f2018-01-26 23:46:06 +0000167 if (m_shouldAdmit == shouldAdmit) {
168 return;
Junxiao Shia9388182014-12-13 23:16:09 -0700169 }
Junxiao Shi3d2049f2018-01-26 23:46:06 +0000170 m_shouldAdmit = shouldAdmit;
171 NFD_LOG_INFO((shouldAdmit ? "Enabling" : "Disabling") << " Data admittance");
172}
173
174void
175Cs::enableServe(bool shouldServe)
176{
177 if (m_shouldServe == shouldServe) {
178 return;
179 }
180 m_shouldServe = shouldServe;
181 NFD_LOG_INFO((shouldServe ? "Enabling" : "Disabling") << " Data serving");
Junxiao Shia9388182014-12-13 23:16:09 -0700182}
183
184} // namespace cs
185} // namespace nfd