blob: a73303f4b6335abe18cfa37f2622607b9157c75b [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 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)
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050047{
Davide Pesavento3dade002019-03-19 11:29:56 -060048 setPolicyImpl(makeDefaultPolicy());
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050049 m_policy->setLimit(nMaxPackets);
Junxiao Shi0fcb41e2014-01-24 10:29:43 -070050}
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080051
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080052void
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080053Cs::insert(const Data& data, bool isUnsolicited)
Junxiao Shi0fcb41e2014-01-24 10:29:43 -070054{
Junxiao Shi3d2049f2018-01-26 23:46:06 +000055 if (!m_shouldAdmit || m_policy->getLimit() == 0) {
Minsheng Zhangffe8bbb2016-03-10 13:40:37 -070056 return;
57 }
Junxiao Shi3d2049f2018-01-26 23:46:06 +000058 NFD_LOG_DEBUG("insert " << data.getName());
Minsheng Zhangffe8bbb2016-03-10 13:40:37 -070059
Junxiao Shi0de23a22015-12-03 20:07:02 +000060 // recognize CachePolicy
61 shared_ptr<lp::CachePolicyTag> tag = data.getTag<lp::CachePolicyTag>();
62 if (tag != nullptr) {
63 lp::CachePolicyType policy = tag->get().getPolicy();
64 if (policy == lp::CachePolicyType::NO_CACHE) {
Minsheng Zhangffe8bbb2016-03-10 13:40:37 -070065 return;
Junxiao Shi35b16b12015-02-16 22:14:57 -070066 }
67 }
68
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050069 iterator it;
Junxiao Shi3d2049f2018-01-26 23:46:06 +000070 bool isNewEntry = false;
71 std::tie(it, isNewEntry) = m_table.emplace(data.shared_from_this(), isUnsolicited);
Junxiao Shi5e4a02f2019-07-15 11:59:18 +000072 Entry& entry = const_cast<Entry&>(*it);
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080073
Junxiao Shi5e4a02f2019-07-15 11:59:18 +000074 entry.updateFreshUntil();
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050075
Junxiao Shia9388182014-12-13 23:16:09 -070076 if (!isNewEntry) { // existing entry
Junxiao Shia9388182014-12-13 23:16:09 -070077 // XXX This doesn't forbid unsolicited Data from refreshing a solicited entry.
78 if (entry.isUnsolicited() && !isUnsolicited) {
Junxiao Shi5e4a02f2019-07-15 11:59:18 +000079 entry.clearUnsolicited();
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080080 }
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050081
82 m_policy->afterRefresh(it);
Junxiao Shiaf6569a2014-06-14 00:01:34 -070083 }
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050084 else {
85 m_policy->afterInsert(it);
86 }
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080087}
88
Junxiao Shi14b39182019-04-29 19:19:18 +000089std::pair<iterator, iterator>
90Cs::findPrefixRange(const Name& prefix) const
Junxiao Shi30c37ab2018-04-09 14:26:47 +000091{
Junxiao Shi30c37ab2018-04-09 14:26:47 +000092 iterator first = m_table.lower_bound(prefix);
93 iterator last = m_table.end();
94 if (prefix.size() > 0) {
95 last = m_table.lower_bound(prefix.getSuccessor());
96 }
Junxiao Shi14b39182019-04-29 19:19:18 +000097 return {first, last};
98}
99
100size_t
101Cs::eraseImpl(const Name& prefix, size_t limit)
102{
103 iterator i, last;
104 std::tie(i, last) = findPrefixRange(prefix);
Junxiao Shi30c37ab2018-04-09 14:26:47 +0000105
106 size_t nErased = 0;
Junxiao Shi14b39182019-04-29 19:19:18 +0000107 while (i != last && nErased < limit) {
108 m_policy->beforeErase(i);
109 i = m_table.erase(i);
Junxiao Shi30c37ab2018-04-09 14:26:47 +0000110 ++nErased;
111 }
Junxiao Shi14b39182019-04-29 19:19:18 +0000112 return nErased;
Junxiao Shi30c37ab2018-04-09 14:26:47 +0000113}
114
Junxiao Shi14b39182019-04-29 19:19:18 +0000115iterator
116Cs::findImpl(const Interest& interest) const
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800117{
Junxiao Shi3d2049f2018-01-26 23:46:06 +0000118 if (!m_shouldServe || m_policy->getLimit() == 0) {
Junxiao Shi14b39182019-04-29 19:19:18 +0000119 return m_table.end();
Junxiao Shi3d2049f2018-01-26 23:46:06 +0000120 }
Junxiao Shi14b39182019-04-29 19:19:18 +0000121
Junxiao Shia9388182014-12-13 23:16:09 -0700122 const Name& prefix = interest.getName();
Junxiao Shi14b39182019-04-29 19:19:18 +0000123 auto range = findPrefixRange(prefix);
124 auto match = std::find_if(range.first, range.second,
125 [&interest] (const auto& entry) { return entry.canSatisfy(interest); });
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800126
Junxiao Shi14b39182019-04-29 19:19:18 +0000127 if (match == range.second) {
128 NFD_LOG_DEBUG("find " << prefix << " no-match");
129 return m_table.end();
Junxiao Shia9388182014-12-13 23:16:09 -0700130 }
Junxiao Shi14b39182019-04-29 19:19:18 +0000131 NFD_LOG_DEBUG("find " << prefix << " matching " << match->getName());
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500132 m_policy->beforeUse(match);
Junxiao Shi14b39182019-04-29 19:19:18 +0000133 return match;
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800134}
135
136void
Junxiao Shi3d2049f2018-01-26 23:46:06 +0000137Cs::dump()
138{
139 NFD_LOG_DEBUG("dump table");
Junxiao Shi5e4a02f2019-07-15 11:59:18 +0000140 for (const Entry& entry : m_table) {
Junxiao Shi3d2049f2018-01-26 23:46:06 +0000141 NFD_LOG_TRACE(entry.getFullName());
142 }
143}
144
145void
146Cs::setPolicy(unique_ptr<Policy> policy)
147{
148 BOOST_ASSERT(policy != nullptr);
149 BOOST_ASSERT(m_policy != nullptr);
150 size_t limit = m_policy->getLimit();
151 this->setPolicyImpl(std::move(policy));
152 m_policy->setLimit(limit);
153}
154
155void
Junxiao Shi52555b02016-11-25 21:39:06 +0000156Cs::setPolicyImpl(unique_ptr<Policy> policy)
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800157{
Junxiao Shi52555b02016-11-25 21:39:06 +0000158 NFD_LOG_DEBUG("set-policy " << policy->getName());
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500159 m_policy = std::move(policy);
160 m_beforeEvictConnection = m_policy->beforeEvict.connect([this] (iterator it) {
161 m_table.erase(it);
162 });
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800163
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500164 m_policy->setCs(this);
165 BOOST_ASSERT(m_policy->getCs() == this);
Junxiao Shia9388182014-12-13 23:16:09 -0700166}
167
168void
Junxiao Shi3d2049f2018-01-26 23:46:06 +0000169Cs::enableAdmit(bool shouldAdmit)
Junxiao Shia9388182014-12-13 23:16:09 -0700170{
Junxiao Shi3d2049f2018-01-26 23:46:06 +0000171 if (m_shouldAdmit == shouldAdmit) {
172 return;
Junxiao Shia9388182014-12-13 23:16:09 -0700173 }
Junxiao Shi3d2049f2018-01-26 23:46:06 +0000174 m_shouldAdmit = shouldAdmit;
175 NFD_LOG_INFO((shouldAdmit ? "Enabling" : "Disabling") << " Data admittance");
176}
177
178void
179Cs::enableServe(bool shouldServe)
180{
181 if (m_shouldServe == shouldServe) {
182 return;
183 }
184 m_shouldServe = shouldServe;
185 NFD_LOG_INFO((shouldServe ? "Enabling" : "Disabling") << " Data serving");
Junxiao Shia9388182014-12-13 23:16:09 -0700186}
187
188} // namespace cs
189} // namespace nfd