blob: d93cc91891cbacb3576b7f1101ec08d45512e7c2 [file] [log] [blame]
Junxiao Shi65d00722014-02-17 10:50:20 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shib184e532016-05-26 18:09:57 +00003 * Copyright (c) 2014-2016, Regents of the University of California,
Junxiao Shi767cb332015-01-08 09:35:49 -07004 * Arizona Board of Regents,
5 * Colorado State University,
Junxiao Shi35353962015-01-08 09:13:47 -07006 * University Pierre & Marie Curie, Sorbonne University,
Junxiao Shi767cb332015-01-08 09:35:49 -07007 * Washington University in St. Louis,
8 * Beijing Institute of Technology,
9 * The University of Memphis.
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070010 *
11 * 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 Shi19838042014-06-21 00:34:01 -070024 */
Junxiao Shi65d00722014-02-17 10:50:20 -070025
26#include "measurements.hpp"
HangZhangc85a23c2014-03-01 15:55:55 +080027#include "name-tree.hpp"
Junxiao Shi65d00722014-02-17 10:50:20 -070028#include "pit-entry.hpp"
HangZhangc85a23c2014-03-01 15:55:55 +080029#include "fib-entry.hpp"
Junxiao Shi65d00722014-02-17 10:50:20 -070030
31namespace nfd {
Junxiao Shi80f9fcd2016-07-23 02:48:36 +000032namespace measurements {
Junxiao Shib30c7b02015-01-07 15:45:54 -070033
HangZhangc85a23c2014-03-01 15:55:55 +080034Measurements::Measurements(NameTree& nameTree)
35 : m_nameTree(nameTree)
36 , m_nItems(0)
Junxiao Shi65d00722014-02-17 10:50:20 -070037{
38}
39
Junxiao Shi80f9fcd2016-07-23 02:48:36 +000040Entry&
Junxiao Shie368d992014-12-02 23:44:31 -070041Measurements::get(name_tree::Entry& nte)
Junxiao Shi65d00722014-02-17 10:50:20 -070042{
Junxiao Shi80f9fcd2016-07-23 02:48:36 +000043 Entry* entry = nte.getMeasurementsEntry();
44 if (entry != nullptr) {
45 return *entry;
46 }
Junxiao Shi19838042014-06-21 00:34:01 -070047
Junxiao Shie3cf2852016-08-09 03:50:56 +000048 nte.setMeasurementsEntry(make_unique<Entry>(nte.getName()));
Junxiao Shi19838042014-06-21 00:34:01 -070049 ++m_nItems;
Junxiao Shi80f9fcd2016-07-23 02:48:36 +000050 entry = nte.getMeasurementsEntry();
Junxiao Shi19838042014-06-21 00:34:01 -070051
52 entry->m_expiry = time::steady_clock::now() + getInitialLifetime();
53 entry->m_cleanup = scheduler::schedule(getInitialLifetime(),
Junxiao Shie368d992014-12-02 23:44:31 -070054 bind(&Measurements::cleanup, this, ref(*entry)));
Junxiao Shi19838042014-06-21 00:34:01 -070055
Junxiao Shi80f9fcd2016-07-23 02:48:36 +000056 return *entry;
Junxiao Shi65d00722014-02-17 10:50:20 -070057}
58
Junxiao Shi80f9fcd2016-07-23 02:48:36 +000059Entry&
HangZhangcb4fc832014-03-11 16:57:11 +080060Measurements::get(const Name& name)
61{
Junxiao Shie368d992014-12-02 23:44:31 -070062 shared_ptr<name_tree::Entry> nte = m_nameTree.lookup(name);
Junxiao Shi80f9fcd2016-07-23 02:48:36 +000063 BOOST_ASSERT(nte != nullptr);
Junxiao Shie368d992014-12-02 23:44:31 -070064 return this->get(*nte);
HangZhangcb4fc832014-03-11 16:57:11 +080065}
66
Junxiao Shi80f9fcd2016-07-23 02:48:36 +000067Entry&
Junxiao Shi65d00722014-02-17 10:50:20 -070068Measurements::get(const fib::Entry& fibEntry)
69{
Junxiao Shib184e532016-05-26 18:09:57 +000070 shared_ptr<name_tree::Entry> nte = m_nameTree.lookup(fibEntry);
Junxiao Shi8c0500f2015-11-11 08:30:16 -070071 if (nte == nullptr) {
Junxiao Shi80f9fcd2016-07-23 02:48:36 +000072 // must be Fib::s_emptyEntry that is unattached
Junxiao Shi8c0500f2015-11-11 08:30:16 -070073 BOOST_ASSERT(fibEntry.getPrefix().empty());
74 nte = m_nameTree.lookup(fibEntry.getPrefix());
75 }
76
77 BOOST_ASSERT(nte != nullptr);
Junxiao Shie368d992014-12-02 23:44:31 -070078 return this->get(*nte);
Junxiao Shi65d00722014-02-17 10:50:20 -070079}
80
Junxiao Shi80f9fcd2016-07-23 02:48:36 +000081Entry&
Junxiao Shi65d00722014-02-17 10:50:20 -070082Measurements::get(const pit::Entry& pitEntry)
83{
Junxiao Shib184e532016-05-26 18:09:57 +000084 shared_ptr<name_tree::Entry> nte = m_nameTree.lookup(pitEntry);
Junxiao Shi8c0500f2015-11-11 08:30:16 -070085 BOOST_ASSERT(nte != nullptr);
Junxiao Shie368d992014-12-02 23:44:31 -070086 return this->get(*nte);
Junxiao Shi65d00722014-02-17 10:50:20 -070087}
88
Junxiao Shi80f9fcd2016-07-23 02:48:36 +000089Entry*
Junxiao Shib30c7b02015-01-07 15:45:54 -070090Measurements::getParent(const Entry& child)
Junxiao Shi65d00722014-02-17 10:50:20 -070091{
Junxiao Shi80f9fcd2016-07-23 02:48:36 +000092 if (child.getName().empty()) { // the root entry
Junxiao Shie368d992014-12-02 23:44:31 -070093 return nullptr;
Junxiao Shi65d00722014-02-17 10:50:20 -070094 }
95
Junxiao Shib184e532016-05-26 18:09:57 +000096 shared_ptr<name_tree::Entry> nteChild = m_nameTree.lookup(child);
Junxiao Shib660b4c2016-08-06 20:47:44 +000097 name_tree::Entry* nte = nteChild->getParent();
Junxiao Shie368d992014-12-02 23:44:31 -070098 BOOST_ASSERT(nte != nullptr);
Junxiao Shi80f9fcd2016-07-23 02:48:36 +000099 return &this->get(*nte);
Junxiao Shi65d00722014-02-17 10:50:20 -0700100}
101
Junxiao Shi767cb332015-01-08 09:35:49 -0700102template<typename K>
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000103Entry*
Junxiao Shi767cb332015-01-08 09:35:49 -0700104Measurements::findLongestPrefixMatchImpl(const K& key,
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000105 const EntryPredicate& pred) const
Junxiao Shi65d00722014-02-17 10:50:20 -0700106{
Junxiao Shi767cb332015-01-08 09:35:49 -0700107 shared_ptr<name_tree::Entry> match = m_nameTree.findLongestPrefixMatch(key,
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000108 [&pred] (const name_tree::Entry& nte) -> bool {
109 Entry* entry = nte.getMeasurementsEntry();
Junxiao Shib30c7b02015-01-07 15:45:54 -0700110 return entry != nullptr && pred(*entry);
111 });
Junxiao Shi767cb332015-01-08 09:35:49 -0700112 if (match != nullptr) {
113 return match->getMeasurementsEntry();
Junxiao Shi65d00722014-02-17 10:50:20 -0700114 }
Junxiao Shie368d992014-12-02 23:44:31 -0700115 return nullptr;
HangZhangc85a23c2014-03-01 15:55:55 +0800116}
Junxiao Shi65d00722014-02-17 10:50:20 -0700117
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000118Entry*
Junxiao Shi767cb332015-01-08 09:35:49 -0700119Measurements::findLongestPrefixMatch(const Name& name,
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000120 const EntryPredicate& pred) const
Junxiao Shi767cb332015-01-08 09:35:49 -0700121{
122 return this->findLongestPrefixMatchImpl(name, pred);
123}
124
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000125Entry*
Junxiao Shi767cb332015-01-08 09:35:49 -0700126Measurements::findLongestPrefixMatch(const pit::Entry& pitEntry,
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000127 const EntryPredicate& pred) const
Junxiao Shi767cb332015-01-08 09:35:49 -0700128{
Junxiao Shib184e532016-05-26 18:09:57 +0000129 shared_ptr<name_tree::Entry> nte = m_nameTree.lookup(pitEntry);
130 BOOST_ASSERT(nte != nullptr);
Junxiao Shi767cb332015-01-08 09:35:49 -0700131 return this->findLongestPrefixMatchImpl(nte, pred);
132}
133
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000134Entry*
HangZhangc85a23c2014-03-01 15:55:55 +0800135Measurements::findExactMatch(const Name& name) const
136{
Junxiao Shie368d992014-12-02 23:44:31 -0700137 shared_ptr<name_tree::Entry> nte = m_nameTree.lookup(name);
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000138 if (nte != nullptr) {
Junxiao Shie368d992014-12-02 23:44:31 -0700139 return nte->getMeasurementsEntry();
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000140 }
Junxiao Shie368d992014-12-02 23:44:31 -0700141 return nullptr;
Junxiao Shi65d00722014-02-17 10:50:20 -0700142}
143
144void
Junxiao Shib184e532016-05-26 18:09:57 +0000145Measurements::extendLifetime(Entry& entry, const time::nanoseconds& lifetime)
Junxiao Shi65d00722014-02-17 10:50:20 -0700146{
Junxiao Shib184e532016-05-26 18:09:57 +0000147 shared_ptr<name_tree::Entry> nte = m_nameTree.lookup(entry);
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000148 BOOST_ASSERT(nte != nullptr);
Junxiao Shiee5a4442014-07-27 17:13:43 -0700149
150 time::steady_clock::TimePoint expiry = time::steady_clock::now() + lifetime;
Junxiao Shie368d992014-12-02 23:44:31 -0700151 if (entry.m_expiry >= expiry) {
Junxiao Shiee5a4442014-07-27 17:13:43 -0700152 // has longer lifetime, not extending
153 return;
154 }
155
Junxiao Shie368d992014-12-02 23:44:31 -0700156 scheduler::cancel(entry.m_cleanup);
157 entry.m_expiry = expiry;
158 entry.m_cleanup = scheduler::schedule(lifetime, bind(&Measurements::cleanup, this, ref(entry)));
HangZhangc85a23c2014-03-01 15:55:55 +0800159}
160
161void
Junxiao Shib30c7b02015-01-07 15:45:54 -0700162Measurements::cleanup(Entry& entry)
HangZhangc85a23c2014-03-01 15:55:55 +0800163{
Junxiao Shib184e532016-05-26 18:09:57 +0000164 shared_ptr<name_tree::Entry> nte = m_nameTree.lookup(entry);
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000165 BOOST_ASSERT(nte != nullptr);
166
167 nte->setMeasurementsEntry(nullptr);
Junxiao Shie3cf2852016-08-09 03:50:56 +0000168 m_nameTree.eraseIfEmpty(nte.get());
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000169 --m_nItems;
Junxiao Shi65d00722014-02-17 10:50:20 -0700170}
171
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000172} // namespace measurements
Junxiao Shi65d00722014-02-17 10:50:20 -0700173} // namespace nfd