Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Junxiao Shi | 767cb33 | 2015-01-08 09:35:49 -0700 | [diff] [blame] | 3 | * Copyright (c) 2014-2015, Regents of the University of California, |
| 4 | * Arizona Board of Regents, |
| 5 | * Colorado State University, |
Junxiao Shi | 3535396 | 2015-01-08 09:13:47 -0700 | [diff] [blame] | 6 | * University Pierre & Marie Curie, Sorbonne University, |
Junxiao Shi | 767cb33 | 2015-01-08 09:35:49 -0700 | [diff] [blame] | 7 | * Washington University in St. Louis, |
| 8 | * Beijing Institute of Technology, |
| 9 | * The University of Memphis. |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 10 | * |
| 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 Shi | 1983804 | 2014-06-21 00:34:01 -0700 | [diff] [blame] | 24 | */ |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 25 | |
| 26 | #include "measurements.hpp" |
HangZhang | c85a23c | 2014-03-01 15:55:55 +0800 | [diff] [blame] | 27 | #include "name-tree.hpp" |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 28 | #include "pit-entry.hpp" |
HangZhang | c85a23c | 2014-03-01 15:55:55 +0800 | [diff] [blame] | 29 | #include "fib-entry.hpp" |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 30 | |
| 31 | namespace nfd { |
| 32 | |
Junxiao Shi | b30c7b0 | 2015-01-07 15:45:54 -0700 | [diff] [blame] | 33 | using measurements::Entry; |
| 34 | |
HangZhang | c85a23c | 2014-03-01 15:55:55 +0800 | [diff] [blame] | 35 | Measurements::Measurements(NameTree& nameTree) |
| 36 | : m_nameTree(nameTree) |
| 37 | , m_nItems(0) |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 38 | { |
| 39 | } |
| 40 | |
Junxiao Shi | b30c7b0 | 2015-01-07 15:45:54 -0700 | [diff] [blame] | 41 | shared_ptr<Entry> |
Junxiao Shi | e368d99 | 2014-12-02 23:44:31 -0700 | [diff] [blame] | 42 | Measurements::get(name_tree::Entry& nte) |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 43 | { |
Junxiao Shi | b30c7b0 | 2015-01-07 15:45:54 -0700 | [diff] [blame] | 44 | shared_ptr<Entry> entry = nte.getMeasurementsEntry(); |
Junxiao Shi | e368d99 | 2014-12-02 23:44:31 -0700 | [diff] [blame] | 45 | if (entry != nullptr) |
HangZhang | c85a23c | 2014-03-01 15:55:55 +0800 | [diff] [blame] | 46 | return entry; |
Junxiao Shi | 1983804 | 2014-06-21 00:34:01 -0700 | [diff] [blame] | 47 | |
Junxiao Shi | b30c7b0 | 2015-01-07 15:45:54 -0700 | [diff] [blame] | 48 | entry = make_shared<Entry>(nte.getPrefix()); |
Junxiao Shi | e368d99 | 2014-12-02 23:44:31 -0700 | [diff] [blame] | 49 | nte.setMeasurementsEntry(entry); |
Junxiao Shi | 1983804 | 2014-06-21 00:34:01 -0700 | [diff] [blame] | 50 | ++m_nItems; |
| 51 | |
| 52 | entry->m_expiry = time::steady_clock::now() + getInitialLifetime(); |
| 53 | entry->m_cleanup = scheduler::schedule(getInitialLifetime(), |
Junxiao Shi | e368d99 | 2014-12-02 23:44:31 -0700 | [diff] [blame] | 54 | bind(&Measurements::cleanup, this, ref(*entry))); |
Junxiao Shi | 1983804 | 2014-06-21 00:34:01 -0700 | [diff] [blame] | 55 | |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 56 | return entry; |
| 57 | } |
| 58 | |
Junxiao Shi | b30c7b0 | 2015-01-07 15:45:54 -0700 | [diff] [blame] | 59 | shared_ptr<Entry> |
HangZhang | cb4fc83 | 2014-03-11 16:57:11 +0800 | [diff] [blame] | 60 | Measurements::get(const Name& name) |
| 61 | { |
Junxiao Shi | e368d99 | 2014-12-02 23:44:31 -0700 | [diff] [blame] | 62 | shared_ptr<name_tree::Entry> nte = m_nameTree.lookup(name); |
| 63 | return this->get(*nte); |
HangZhang | cb4fc83 | 2014-03-11 16:57:11 +0800 | [diff] [blame] | 64 | } |
| 65 | |
Junxiao Shi | b30c7b0 | 2015-01-07 15:45:54 -0700 | [diff] [blame] | 66 | shared_ptr<Entry> |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 67 | Measurements::get(const fib::Entry& fibEntry) |
| 68 | { |
Junxiao Shi | e368d99 | 2014-12-02 23:44:31 -0700 | [diff] [blame] | 69 | shared_ptr<name_tree::Entry> nte = m_nameTree.get(fibEntry); |
Junxiao Shi | 8c0500f | 2015-11-11 08:30:16 -0700 | [diff] [blame^] | 70 | if (nte == nullptr) { |
| 71 | // must be Fib::s_emptyEntry that is unattched |
| 72 | BOOST_ASSERT(fibEntry.getPrefix().empty()); |
| 73 | nte = m_nameTree.lookup(fibEntry.getPrefix()); |
| 74 | } |
| 75 | |
| 76 | BOOST_ASSERT(nte != nullptr); |
Junxiao Shi | e368d99 | 2014-12-02 23:44:31 -0700 | [diff] [blame] | 77 | return this->get(*nte); |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 78 | } |
| 79 | |
Junxiao Shi | b30c7b0 | 2015-01-07 15:45:54 -0700 | [diff] [blame] | 80 | shared_ptr<Entry> |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 81 | Measurements::get(const pit::Entry& pitEntry) |
| 82 | { |
Junxiao Shi | e368d99 | 2014-12-02 23:44:31 -0700 | [diff] [blame] | 83 | shared_ptr<name_tree::Entry> nte = m_nameTree.get(pitEntry); |
Junxiao Shi | 8c0500f | 2015-11-11 08:30:16 -0700 | [diff] [blame^] | 84 | |
| 85 | BOOST_ASSERT(nte != nullptr); |
Junxiao Shi | e368d99 | 2014-12-02 23:44:31 -0700 | [diff] [blame] | 86 | return this->get(*nte); |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 87 | } |
| 88 | |
Junxiao Shi | b30c7b0 | 2015-01-07 15:45:54 -0700 | [diff] [blame] | 89 | shared_ptr<Entry> |
| 90 | Measurements::getParent(const Entry& child) |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 91 | { |
Junxiao Shi | e368d99 | 2014-12-02 23:44:31 -0700 | [diff] [blame] | 92 | if (child.getName().size() == 0) { // the root entry |
| 93 | return nullptr; |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 94 | } |
| 95 | |
Junxiao Shi | e368d99 | 2014-12-02 23:44:31 -0700 | [diff] [blame] | 96 | shared_ptr<name_tree::Entry> nteChild = m_nameTree.get(child); |
| 97 | shared_ptr<name_tree::Entry> nte = nteChild->getParent(); |
| 98 | BOOST_ASSERT(nte != nullptr); |
| 99 | return this->get(*nte); |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 100 | } |
| 101 | |
Junxiao Shi | 767cb33 | 2015-01-08 09:35:49 -0700 | [diff] [blame] | 102 | template<typename K> |
Junxiao Shi | b30c7b0 | 2015-01-07 15:45:54 -0700 | [diff] [blame] | 103 | shared_ptr<Entry> |
Junxiao Shi | 767cb33 | 2015-01-08 09:35:49 -0700 | [diff] [blame] | 104 | Measurements::findLongestPrefixMatchImpl(const K& key, |
| 105 | const measurements::EntryPredicate& pred) const |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 106 | { |
Junxiao Shi | 767cb33 | 2015-01-08 09:35:49 -0700 | [diff] [blame] | 107 | shared_ptr<name_tree::Entry> match = m_nameTree.findLongestPrefixMatch(key, |
Junxiao Shi | b30c7b0 | 2015-01-07 15:45:54 -0700 | [diff] [blame] | 108 | [pred] (const name_tree::Entry& nte) -> bool { |
| 109 | shared_ptr<Entry> entry = nte.getMeasurementsEntry(); |
| 110 | return entry != nullptr && pred(*entry); |
| 111 | }); |
Junxiao Shi | 767cb33 | 2015-01-08 09:35:49 -0700 | [diff] [blame] | 112 | if (match != nullptr) { |
| 113 | return match->getMeasurementsEntry(); |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 114 | } |
Junxiao Shi | e368d99 | 2014-12-02 23:44:31 -0700 | [diff] [blame] | 115 | return nullptr; |
HangZhang | c85a23c | 2014-03-01 15:55:55 +0800 | [diff] [blame] | 116 | } |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 117 | |
Junxiao Shi | b30c7b0 | 2015-01-07 15:45:54 -0700 | [diff] [blame] | 118 | shared_ptr<Entry> |
Junxiao Shi | 767cb33 | 2015-01-08 09:35:49 -0700 | [diff] [blame] | 119 | Measurements::findLongestPrefixMatch(const Name& name, |
| 120 | const measurements::EntryPredicate& pred) const |
| 121 | { |
| 122 | return this->findLongestPrefixMatchImpl(name, pred); |
| 123 | } |
| 124 | |
| 125 | shared_ptr<Entry> |
| 126 | Measurements::findLongestPrefixMatch(const pit::Entry& pitEntry, |
| 127 | const measurements::EntryPredicate& pred) const |
| 128 | { |
| 129 | shared_ptr<name_tree::Entry> nte = m_nameTree.get(pitEntry); |
| 130 | return this->findLongestPrefixMatchImpl(nte, pred); |
| 131 | } |
| 132 | |
| 133 | shared_ptr<Entry> |
HangZhang | c85a23c | 2014-03-01 15:55:55 +0800 | [diff] [blame] | 134 | Measurements::findExactMatch(const Name& name) const |
| 135 | { |
Junxiao Shi | e368d99 | 2014-12-02 23:44:31 -0700 | [diff] [blame] | 136 | shared_ptr<name_tree::Entry> nte = m_nameTree.lookup(name); |
| 137 | if (nte != nullptr) |
| 138 | return nte->getMeasurementsEntry(); |
| 139 | return nullptr; |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | void |
Junxiao Shi | b30c7b0 | 2015-01-07 15:45:54 -0700 | [diff] [blame] | 143 | Measurements::extendLifetime(Entry& entry, |
Junxiao Shi | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame] | 144 | const time::nanoseconds& lifetime) |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 145 | { |
Junxiao Shi | e368d99 | 2014-12-02 23:44:31 -0700 | [diff] [blame] | 146 | shared_ptr<name_tree::Entry> nte = m_nameTree.get(entry); |
| 147 | if (nte == nullptr || |
| 148 | nte->getMeasurementsEntry().get() != &entry) { |
Junxiao Shi | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame] | 149 | // entry is already gone; it is a dangling reference |
| 150 | return; |
HangZhang | c85a23c | 2014-03-01 15:55:55 +0800 | [diff] [blame] | 151 | } |
Junxiao Shi | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame] | 152 | |
| 153 | time::steady_clock::TimePoint expiry = time::steady_clock::now() + lifetime; |
Junxiao Shi | e368d99 | 2014-12-02 23:44:31 -0700 | [diff] [blame] | 154 | if (entry.m_expiry >= expiry) { |
Junxiao Shi | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame] | 155 | // has longer lifetime, not extending |
| 156 | return; |
| 157 | } |
| 158 | |
Junxiao Shi | e368d99 | 2014-12-02 23:44:31 -0700 | [diff] [blame] | 159 | scheduler::cancel(entry.m_cleanup); |
| 160 | entry.m_expiry = expiry; |
| 161 | entry.m_cleanup = scheduler::schedule(lifetime, bind(&Measurements::cleanup, this, ref(entry))); |
HangZhang | c85a23c | 2014-03-01 15:55:55 +0800 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | void |
Junxiao Shi | b30c7b0 | 2015-01-07 15:45:54 -0700 | [diff] [blame] | 165 | Measurements::cleanup(Entry& entry) |
HangZhang | c85a23c | 2014-03-01 15:55:55 +0800 | [diff] [blame] | 166 | { |
Junxiao Shi | e368d99 | 2014-12-02 23:44:31 -0700 | [diff] [blame] | 167 | shared_ptr<name_tree::Entry> nte = m_nameTree.get(entry); |
| 168 | if (nte != nullptr) { |
| 169 | nte->setMeasurementsEntry(nullptr); |
| 170 | m_nameTree.eraseEntryIfEmpty(nte); |
HangZhang | c85a23c | 2014-03-01 15:55:55 +0800 | [diff] [blame] | 171 | m_nItems--; |
| 172 | } |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | } // namespace nfd |