Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | 057d149 | 2018-03-20 17:14:18 +0000 | [diff] [blame] | 2 | /* |
Davide Pesavento | 2c9d2ca | 2024-01-27 16:36:51 -0500 | [diff] [blame] | 3 | * Copyright (c) 2014-2024, Regents of the University of California, |
Junxiao Shi | 767cb33 | 2015-01-08 09:35:49 -0700 | [diff] [blame] | 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 | |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 26 | #ifndef NFD_DAEMON_TABLE_MEASUREMENTS_HPP |
| 27 | #define NFD_DAEMON_TABLE_MEASUREMENTS_HPP |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 28 | |
| 29 | #include "measurements-entry.hpp" |
HangZhang | c85a23c | 2014-03-01 15:55:55 +0800 | [diff] [blame] | 30 | #include "name-tree.hpp" |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 31 | |
Davide Pesavento | 2c9d2ca | 2024-01-27 16:36:51 -0500 | [diff] [blame] | 32 | #include <functional> |
| 33 | |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 34 | namespace nfd { |
| 35 | |
HangZhang | c85a23c | 2014-03-01 15:55:55 +0800 | [diff] [blame] | 36 | namespace fib { |
| 37 | class Entry; |
Junxiao Shi | b30c7b0 | 2015-01-07 15:45:54 -0700 | [diff] [blame] | 38 | } // namespace fib |
HangZhang | c85a23c | 2014-03-01 15:55:55 +0800 | [diff] [blame] | 39 | |
| 40 | namespace pit { |
| 41 | class Entry; |
Junxiao Shi | b30c7b0 | 2015-01-07 15:45:54 -0700 | [diff] [blame] | 42 | } // namespace pit |
HangZhang | c85a23c | 2014-03-01 15:55:55 +0800 | [diff] [blame] | 43 | |
Junxiao Shi | b30c7b0 | 2015-01-07 15:45:54 -0700 | [diff] [blame] | 44 | namespace measurements { |
HangZhang | c85a23c | 2014-03-01 15:55:55 +0800 | [diff] [blame] | 45 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 46 | /** |
| 47 | * \brief A predicate that accepts or rejects an entry. |
Junxiao Shi | b30c7b0 | 2015-01-07 15:45:54 -0700 | [diff] [blame] | 48 | */ |
Davide Pesavento | 50a6af3 | 2019-02-21 00:04:40 -0500 | [diff] [blame] | 49 | using EntryPredicate = std::function<bool(const Entry&)>; |
Junxiao Shi | b30c7b0 | 2015-01-07 15:45:54 -0700 | [diff] [blame] | 50 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 51 | /** |
| 52 | * \brief An #EntryPredicate that accepts any entry. |
Junxiao Shi | b30c7b0 | 2015-01-07 15:45:54 -0700 | [diff] [blame] | 53 | */ |
| 54 | class AnyEntry |
| 55 | { |
| 56 | public: |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 57 | constexpr bool |
| 58 | operator()(const Entry&) const noexcept |
Junxiao Shi | b30c7b0 | 2015-01-07 15:45:54 -0700 | [diff] [blame] | 59 | { |
| 60 | return true; |
| 61 | } |
| 62 | }; |
| 63 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 64 | /** |
| 65 | * \brief An #EntryPredicate that accepts an entry if it has StrategyInfo of type T. |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 66 | */ |
Junxiao Shi | b30c7b0 | 2015-01-07 15:45:54 -0700 | [diff] [blame] | 67 | template<typename T> |
| 68 | class EntryWithStrategyInfo |
| 69 | { |
| 70 | public: |
| 71 | bool |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 72 | operator()(const Entry& entry) const |
Junxiao Shi | b30c7b0 | 2015-01-07 15:45:54 -0700 | [diff] [blame] | 73 | { |
| 74 | return entry.getStrategyInfo<T>() != nullptr; |
| 75 | } |
| 76 | }; |
| 77 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 78 | /** |
| 79 | * \brief The %Measurements table. |
Junxiao Shi | 057d149 | 2018-03-20 17:14:18 +0000 | [diff] [blame] | 80 | * |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 81 | * The %Measurements table is a data structure for forwarding strategies to store per name prefix |
| 82 | * measurements. A strategy can access this table via fw::Strategy::getMeasurements(), and then |
| 83 | * place any object that derive from StrategyInfo type onto %Measurements entries. |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 84 | */ |
| 85 | class Measurements : noncopyable |
| 86 | { |
| 87 | public: |
HangZhang | c85a23c | 2014-03-01 15:55:55 +0800 | [diff] [blame] | 88 | explicit |
Junxiao Shi | 057d149 | 2018-03-20 17:14:18 +0000 | [diff] [blame] | 89 | Measurements(NameTree& nameTree); |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 90 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 91 | /** \brief Maximum depth of a %Measurements entry. |
Junxiao Shi | 057d149 | 2018-03-20 17:14:18 +0000 | [diff] [blame] | 92 | */ |
| 93 | static constexpr size_t |
| 94 | getMaxDepth() |
| 95 | { |
| 96 | return NameTree::getMaxDepth(); |
| 97 | } |
| 98 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 99 | /** \brief Find or insert an entry by name. |
Junxiao Shi | 057d149 | 2018-03-20 17:14:18 +0000 | [diff] [blame] | 100 | * |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 101 | * An entry name can have at most getMaxDepth() components. If \p name exceeds this limit, |
| 102 | * it is truncated to the first getMaxDepth() components. |
Junxiao Shi | e368d99 | 2014-12-02 23:44:31 -0700 | [diff] [blame] | 103 | */ |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 104 | Entry& |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 105 | get(const Name& name); |
| 106 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 107 | /** \brief Equivalent to `get(fibEntry.getPrefix())`. |
Junxiao Shi | e368d99 | 2014-12-02 23:44:31 -0700 | [diff] [blame] | 108 | */ |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 109 | Entry& |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 110 | get(const fib::Entry& fibEntry); |
| 111 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 112 | /** \brief Equivalent to `get(pitEntry.getName(), std::min(pitEntry.getName().size(), getMaxDepth()))`. |
Junxiao Shi | e368d99 | 2014-12-02 23:44:31 -0700 | [diff] [blame] | 113 | */ |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 114 | Entry& |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 115 | get(const pit::Entry& pitEntry); |
| 116 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 117 | /** \brief Find or insert a parent entry. |
Junxiao Shi | 057d149 | 2018-03-20 17:14:18 +0000 | [diff] [blame] | 118 | * \retval nullptr child is the root entry |
| 119 | * \return get(child.getName().getPrefix(-1)) |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 120 | */ |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 121 | Entry* |
| 122 | getParent(const Entry& child); |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 123 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 124 | /** \brief Perform a longest prefix match for \p name. |
Junxiao Shi | e368d99 | 2014-12-02 23:44:31 -0700 | [diff] [blame] | 125 | */ |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 126 | Entry* |
Junxiao Shi | b30c7b0 | 2015-01-07 15:45:54 -0700 | [diff] [blame] | 127 | findLongestPrefixMatch(const Name& name, |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 128 | const EntryPredicate& pred = AnyEntry()) const; |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 129 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 130 | /** \brief Perform a longest prefix match for `pitEntry.getName()`. |
Junxiao Shi | 767cb33 | 2015-01-08 09:35:49 -0700 | [diff] [blame] | 131 | */ |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 132 | Entry* |
Junxiao Shi | 767cb33 | 2015-01-08 09:35:49 -0700 | [diff] [blame] | 133 | findLongestPrefixMatch(const pit::Entry& pitEntry, |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 134 | const EntryPredicate& pred = AnyEntry()) const; |
Junxiao Shi | 767cb33 | 2015-01-08 09:35:49 -0700 | [diff] [blame] | 135 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 136 | /** \brief Perform an exact match. |
Junxiao Shi | e368d99 | 2014-12-02 23:44:31 -0700 | [diff] [blame] | 137 | */ |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 138 | Entry* |
HangZhang | c85a23c | 2014-03-01 15:55:55 +0800 | [diff] [blame] | 139 | findExactMatch(const Name& name) const; |
| 140 | |
Junxiao Shi | 1983804 | 2014-06-21 00:34:01 -0700 | [diff] [blame] | 141 | static time::nanoseconds |
Davide Pesavento | 50a6af3 | 2019-02-21 00:04:40 -0500 | [diff] [blame] | 142 | getInitialLifetime() |
| 143 | { |
| 144 | return 4_s; |
| 145 | } |
Junxiao Shi | 1983804 | 2014-06-21 00:34:01 -0700 | [diff] [blame] | 146 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 147 | /** \brief Extend lifetime of an entry. |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 148 | * |
| 149 | * The entry will be kept until at least now()+lifetime. |
| 150 | */ |
| 151 | void |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 152 | extendLifetime(Entry& entry, const time::nanoseconds& lifetime); |
HangZhang | c85a23c | 2014-03-01 15:55:55 +0800 | [diff] [blame] | 153 | |
| 154 | size_t |
Davide Pesavento | 50a6af3 | 2019-02-21 00:04:40 -0500 | [diff] [blame] | 155 | size() const |
| 156 | { |
| 157 | return m_nItems; |
| 158 | } |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 159 | |
| 160 | private: |
| 161 | void |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 162 | cleanup(Entry& entry); |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 163 | |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 164 | Entry& |
Junxiao Shi | e368d99 | 2014-12-02 23:44:31 -0700 | [diff] [blame] | 165 | get(name_tree::Entry& nte); |
HangZhang | cb4fc83 | 2014-03-11 16:57:11 +0800 | [diff] [blame] | 166 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 167 | /** \tparam K a parameter acceptable to NameTree::findLongestPrefixMatch() |
Junxiao Shi | 767cb33 | 2015-01-08 09:35:49 -0700 | [diff] [blame] | 168 | */ |
| 169 | template<typename K> |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 170 | Entry* |
| 171 | findLongestPrefixMatchImpl(const K& key, const EntryPredicate& pred) const; |
Junxiao Shi | 767cb33 | 2015-01-08 09:35:49 -0700 | [diff] [blame] | 172 | |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 173 | private: |
HangZhang | c85a23c | 2014-03-01 15:55:55 +0800 | [diff] [blame] | 174 | NameTree& m_nameTree; |
Davide Pesavento | 50a6af3 | 2019-02-21 00:04:40 -0500 | [diff] [blame] | 175 | size_t m_nItems = 0; |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 176 | }; |
| 177 | |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 178 | } // namespace measurements |
| 179 | |
| 180 | using measurements::Measurements; |
| 181 | |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 182 | } // namespace nfd |
| 183 | |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 184 | #endif // NFD_DAEMON_TABLE_MEASUREMENTS_HPP |