blob: 89c54e99551cd264363721c77bf6511c50102da3 [file] [log] [blame]
Junxiao Shi0fcb41e2014-01-24 10:29:43 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Minsheng Zhangffe8bbb2016-03-10 13:40:37 -07003 * Copyright (c) 2014-2016, 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/asserts.hpp"
29#include "core/logger.hpp"
Junxiao Shicbc8e942016-09-06 03:17:45 +000030#include <ndn-cxx/lp/tags.hpp>
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080031
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080032namespace nfd {
Junxiao Shia9388182014-12-13 23:16:09 -070033namespace cs {
Junxiao Shi0fcb41e2014-01-24 10:29:43 -070034
Junxiao Shi3aba7542016-12-24 02:42:52 +000035NFD_ASSERT_FORWARD_ITERATOR(Cs::const_iterator);
36
37NFD_LOG_INIT("ContentStore");
Junxiao Shifc206962015-01-16 11:12:22 -070038
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050039unique_ptr<Policy>
40makeDefaultPolicy()
Junxiao Shi0fcb41e2014-01-24 10:29:43 -070041{
Junxiao Shi52555b02016-11-25 21:39:06 +000042 const std::string DEFAULT_POLICY = "priority_fifo";
43 return Policy::create(DEFAULT_POLICY);
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{
Junxiao Shib4a5acd2016-12-07 19:59:18 +000048 this->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
53Cs::setLimit(size_t nMaxPackets)
54{
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050055 m_policy->setLimit(nMaxPackets);
56}
57
58size_t
59Cs::getLimit() const
60{
61 return m_policy->getLimit();
62}
63
64void
65Cs::setPolicy(unique_ptr<Policy> policy)
66{
67 BOOST_ASSERT(policy != nullptr);
68 BOOST_ASSERT(m_policy != nullptr);
69 size_t limit = m_policy->getLimit();
Junxiao Shi52555b02016-11-25 21:39:06 +000070 this->setPolicyImpl(std::move(policy));
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050071 m_policy->setLimit(limit);
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080072}
73
Minsheng Zhangffe8bbb2016-03-10 13:40:37 -070074void
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080075Cs::insert(const Data& data, bool isUnsolicited)
Junxiao Shi0fcb41e2014-01-24 10:29:43 -070076{
Junxiao Shi5640ec82015-01-07 21:51:19 -070077 NFD_LOG_DEBUG("insert " << data.getName());
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080078
Minsheng Zhangffe8bbb2016-03-10 13:40:37 -070079 if (m_policy->getLimit() == 0) {
80 // shortcut for disabled CS
81 return;
82 }
83
Junxiao Shi0de23a22015-12-03 20:07:02 +000084 // recognize CachePolicy
85 shared_ptr<lp::CachePolicyTag> tag = data.getTag<lp::CachePolicyTag>();
86 if (tag != nullptr) {
87 lp::CachePolicyType policy = tag->get().getPolicy();
88 if (policy == lp::CachePolicyType::NO_CACHE) {
Minsheng Zhangffe8bbb2016-03-10 13:40:37 -070089 return;
Junxiao Shi35b16b12015-02-16 22:14:57 -070090 }
91 }
92
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050093 bool isNewEntry = false;
94 iterator it;
Junxiao Shia9388182014-12-13 23:16:09 -070095 // use .insert because gcc46 does not support .emplace
Junxiao Shifc206962015-01-16 11:12:22 -070096 std::tie(it, isNewEntry) = m_table.insert(EntryImpl(data.shared_from_this(), isUnsolicited));
97 EntryImpl& entry = const_cast<EntryImpl&>(*it);
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080098
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050099 entry.updateStaleTime();
100
Junxiao Shia9388182014-12-13 23:16:09 -0700101 if (!isNewEntry) { // existing entry
Junxiao Shia9388182014-12-13 23:16:09 -0700102 // XXX This doesn't forbid unsolicited Data from refreshing a solicited entry.
103 if (entry.isUnsolicited() && !isUnsolicited) {
104 entry.unsetUnsolicited();
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800105 }
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500106
107 m_policy->afterRefresh(it);
Junxiao Shiaf6569a2014-06-14 00:01:34 -0700108 }
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500109 else {
110 m_policy->afterInsert(it);
111 }
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800112}
113
mzhang4eab72492015-02-25 11:16:09 -0600114void
115Cs::find(const Interest& interest,
116 const HitCallback& hitCallback,
117 const MissCallback& missCallback) const
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800118{
mzhang4eab72492015-02-25 11:16:09 -0600119 BOOST_ASSERT(static_cast<bool>(hitCallback));
120 BOOST_ASSERT(static_cast<bool>(missCallback));
121
Junxiao Shia9388182014-12-13 23:16:09 -0700122 const Name& prefix = interest.getName();
123 bool isRightmost = interest.getChildSelector() == 1;
124 NFD_LOG_DEBUG("find " << prefix << (isRightmost ? " R" : " L"));
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800125
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500126 iterator first = m_table.lower_bound(prefix);
127 iterator last = m_table.end();
Junxiao Shia9388182014-12-13 23:16:09 -0700128 if (prefix.size() > 0) {
Junxiao Shi5640ec82015-01-07 21:51:19 -0700129 last = m_table.lower_bound(prefix.getSuccessor());
Junxiao Shia9388182014-12-13 23:16:09 -0700130 }
Junxiao Shi5640ec82015-01-07 21:51:19 -0700131
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500132 iterator match = last;
Junxiao Shi5640ec82015-01-07 21:51:19 -0700133 if (isRightmost) {
134 match = this->findRightmost(interest, first, last);
135 }
136 else {
137 match = this->findLeftmost(interest, first, last);
138 }
139
140 if (match == last) {
141 NFD_LOG_DEBUG(" no-match");
mzhang4eab72492015-02-25 11:16:09 -0600142 missCallback(interest);
143 return;
Junxiao Shi5640ec82015-01-07 21:51:19 -0700144 }
145 NFD_LOG_DEBUG(" matching " << match->getName());
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500146 m_policy->beforeUse(match);
mzhang4eab72492015-02-25 11:16:09 -0600147 hitCallback(interest, match->getData());
Junxiao Shi5640ec82015-01-07 21:51:19 -0700148}
149
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500150iterator
151Cs::findLeftmost(const Interest& interest, iterator first, iterator last) const
Junxiao Shi5640ec82015-01-07 21:51:19 -0700152{
Junxiao Shifc206962015-01-16 11:12:22 -0700153 return std::find_if(first, last, bind(&cs::EntryImpl::canSatisfy, _1, interest));
Junxiao Shi5640ec82015-01-07 21:51:19 -0700154}
155
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500156iterator
157Cs::findRightmost(const Interest& interest, iterator first, iterator last) const
Junxiao Shi5640ec82015-01-07 21:51:19 -0700158{
159 // Each loop visits a sub-namespace under a prefix one component longer than Interest Name.
160 // If there is a match in that sub-namespace, the leftmost match is returned;
161 // otherwise, loop continues.
162
163 size_t interestNameLength = interest.getName().size();
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500164 for (iterator right = last; right != first;) {
165 iterator prev = std::prev(right);
Junxiao Shi5640ec82015-01-07 21:51:19 -0700166
167 // special case: [first,prev] have exact Names
168 if (prev->getName().size() == interestNameLength) {
169 NFD_LOG_TRACE(" find-among-exact " << prev->getName());
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500170 iterator matchExact = this->findRightmostAmongExact(interest, first, right);
Junxiao Shi5640ec82015-01-07 21:51:19 -0700171 return matchExact == right ? last : matchExact;
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800172 }
173
Junxiao Shi5640ec82015-01-07 21:51:19 -0700174 Name prefix = prev->getName().getPrefix(interestNameLength + 1);
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500175 iterator left = m_table.lower_bound(prefix);
Junxiao Shi5640ec82015-01-07 21:51:19 -0700176
177 // normal case: [left,right) are under one-component-longer prefix
178 NFD_LOG_TRACE(" find-under-prefix " << prefix);
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500179 iterator match = this->findLeftmost(interest, left, right);
Junxiao Shi5640ec82015-01-07 21:51:19 -0700180 if (match != right) {
181 return match;
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800182 }
Junxiao Shi5640ec82015-01-07 21:51:19 -0700183 right = left;
Junxiao Shia9388182014-12-13 23:16:09 -0700184 }
Junxiao Shi5640ec82015-01-07 21:51:19 -0700185 return last;
186}
187
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500188iterator
189Cs::findRightmostAmongExact(const Interest& interest, iterator first, iterator last) const
Junxiao Shi5640ec82015-01-07 21:51:19 -0700190{
Junxiao Shifc206962015-01-16 11:12:22 -0700191 return find_last_if(first, last, bind(&EntryImpl::canSatisfy, _1, interest));
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800192}
193
194void
Junxiao Shi52555b02016-11-25 21:39:06 +0000195Cs::setPolicyImpl(unique_ptr<Policy> policy)
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800196{
Junxiao Shi52555b02016-11-25 21:39:06 +0000197 NFD_LOG_DEBUG("set-policy " << policy->getName());
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500198 m_policy = std::move(policy);
199 m_beforeEvictConnection = m_policy->beforeEvict.connect([this] (iterator it) {
200 m_table.erase(it);
201 });
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800202
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500203 m_policy->setCs(this);
204 BOOST_ASSERT(m_policy->getCs() == this);
Junxiao Shia9388182014-12-13 23:16:09 -0700205}
206
207void
208Cs::dump()
209{
210 NFD_LOG_DEBUG("dump table");
Junxiao Shifc206962015-01-16 11:12:22 -0700211 for (const EntryImpl& entry : m_table) {
Junxiao Shia9388182014-12-13 23:16:09 -0700212 NFD_LOG_TRACE(entry.getFullName());
213 }
214}
215
216} // namespace cs
217} // namespace nfd