blob: 342d75377ca790916d5eadb48f5c0e9c3b910cb2 [file] [log] [blame]
Junxiao Shi65d00722014-02-17 10:50:20 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shi057d1492018-03-20 17:14:18 +00002/*
Davide Pesavento50a6af32019-02-21 00:04:40 -05003 * Copyright (c) 2014-2019, 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
Alexander Afanasyev613e2a92014-04-15 13:36:58 -070026#ifndef NFD_DAEMON_TABLE_MEASUREMENTS_HPP
27#define NFD_DAEMON_TABLE_MEASUREMENTS_HPP
Junxiao Shi65d00722014-02-17 10:50:20 -070028
29#include "measurements-entry.hpp"
HangZhangc85a23c2014-03-01 15:55:55 +080030#include "name-tree.hpp"
Junxiao Shi65d00722014-02-17 10:50:20 -070031
32namespace nfd {
33
HangZhangc85a23c2014-03-01 15:55:55 +080034namespace fib {
35class Entry;
Junxiao Shib30c7b02015-01-07 15:45:54 -070036} // namespace fib
HangZhangc85a23c2014-03-01 15:55:55 +080037
38namespace pit {
39class Entry;
Junxiao Shib30c7b02015-01-07 15:45:54 -070040} // namespace pit
HangZhangc85a23c2014-03-01 15:55:55 +080041
Junxiao Shib30c7b02015-01-07 15:45:54 -070042namespace measurements {
HangZhangc85a23c2014-03-01 15:55:55 +080043
Davide Pesavento50a6af32019-02-21 00:04:40 -050044/** \brief A predicate that accepts or rejects an entry
Junxiao Shib30c7b02015-01-07 15:45:54 -070045 */
Davide Pesavento50a6af32019-02-21 00:04:40 -050046using EntryPredicate = std::function<bool(const Entry&)>;
Junxiao Shib30c7b02015-01-07 15:45:54 -070047
Davide Pesavento50a6af32019-02-21 00:04:40 -050048/** \brief An \c EntryPredicate that accepts any entry
Junxiao Shib30c7b02015-01-07 15:45:54 -070049 */
50class AnyEntry
51{
52public:
53 bool
Davide Pesavento50a6af32019-02-21 00:04:40 -050054 operator()(const Entry&) const
Junxiao Shib30c7b02015-01-07 15:45:54 -070055 {
56 return true;
57 }
58};
59
Davide Pesavento50a6af32019-02-21 00:04:40 -050060/** \brief An \c EntryPredicate that accepts an entry if it has StrategyInfo of type T
Junxiao Shi80f9fcd2016-07-23 02:48:36 +000061 */
Junxiao Shib30c7b02015-01-07 15:45:54 -070062template<typename T>
63class EntryWithStrategyInfo
64{
65public:
66 bool
Junxiao Shi80f9fcd2016-07-23 02:48:36 +000067 operator()(const Entry& entry) const
Junxiao Shib30c7b02015-01-07 15:45:54 -070068 {
69 return entry.getStrategyInfo<T>() != nullptr;
70 }
71};
72
Davide Pesavento50a6af32019-02-21 00:04:40 -050073/** \brief The Measurements table
Junxiao Shi057d1492018-03-20 17:14:18 +000074 *
75 * The Measurements table is a data structure for forwarding strategies to store per name prefix
76 * measurements. A strategy can access this table via \c Strategy::getMeasurements(), and then
77 * place any object that derive from \c StrategyInfo type onto Measurements entries.
Junxiao Shi65d00722014-02-17 10:50:20 -070078 */
79class Measurements : noncopyable
80{
81public:
HangZhangc85a23c2014-03-01 15:55:55 +080082 explicit
Junxiao Shi057d1492018-03-20 17:14:18 +000083 Measurements(NameTree& nameTree);
Junxiao Shi65d00722014-02-17 10:50:20 -070084
Junxiao Shi057d1492018-03-20 17:14:18 +000085 /** \brief maximum depth of a Measurements entry
86 */
87 static constexpr size_t
88 getMaxDepth()
89 {
90 return NameTree::getMaxDepth();
91 }
92
Davide Pesavento50a6af32019-02-21 00:04:40 -050093 /** \brief Find or insert an entry by name
Junxiao Shi057d1492018-03-20 17:14:18 +000094 *
95 * An entry name can have at most \c getMaxDepth() components. If \p name exceeds this limit,
96 * it is truncated to the first \c getMaxDepth() components.
Junxiao Shie368d992014-12-02 23:44:31 -070097 */
Junxiao Shi80f9fcd2016-07-23 02:48:36 +000098 Entry&
Junxiao Shi65d00722014-02-17 10:50:20 -070099 get(const Name& name);
100
Davide Pesavento50a6af32019-02-21 00:04:40 -0500101 /** \brief Equivalent to `get(fibEntry.getPrefix())`
Junxiao Shie368d992014-12-02 23:44:31 -0700102 */
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000103 Entry&
Junxiao Shi65d00722014-02-17 10:50:20 -0700104 get(const fib::Entry& fibEntry);
105
Davide Pesavento50a6af32019-02-21 00:04:40 -0500106 /** \brief Equivalent to `get(pitEntry.getName(), std::min(pitEntry.getName().size(), getMaxDepth()))`
Junxiao Shie368d992014-12-02 23:44:31 -0700107 */
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000108 Entry&
Junxiao Shi65d00722014-02-17 10:50:20 -0700109 get(const pit::Entry& pitEntry);
110
Davide Pesavento50a6af32019-02-21 00:04:40 -0500111 /** \brief Find or insert a parent entry
Junxiao Shi057d1492018-03-20 17:14:18 +0000112 * \retval nullptr child is the root entry
113 * \return get(child.getName().getPrefix(-1))
Junxiao Shi65d00722014-02-17 10:50:20 -0700114 */
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000115 Entry*
116 getParent(const Entry& child);
Junxiao Shi65d00722014-02-17 10:50:20 -0700117
Davide Pesavento50a6af32019-02-21 00:04:40 -0500118 /** \brief Perform a longest prefix match for \p name
Junxiao Shie368d992014-12-02 23:44:31 -0700119 */
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000120 Entry*
Junxiao Shib30c7b02015-01-07 15:45:54 -0700121 findLongestPrefixMatch(const Name& name,
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000122 const EntryPredicate& pred = AnyEntry()) const;
Junxiao Shi65d00722014-02-17 10:50:20 -0700123
Davide Pesavento50a6af32019-02-21 00:04:40 -0500124 /** \brief Perform a longest prefix match for `pitEntry.getName()`
Junxiao Shi767cb332015-01-08 09:35:49 -0700125 */
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000126 Entry*
Junxiao Shi767cb332015-01-08 09:35:49 -0700127 findLongestPrefixMatch(const pit::Entry& pitEntry,
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000128 const EntryPredicate& pred = AnyEntry()) const;
Junxiao Shi767cb332015-01-08 09:35:49 -0700129
Davide Pesavento50a6af32019-02-21 00:04:40 -0500130 /** \brief Perform an exact match
Junxiao Shie368d992014-12-02 23:44:31 -0700131 */
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000132 Entry*
HangZhangc85a23c2014-03-01 15:55:55 +0800133 findExactMatch(const Name& name) const;
134
Junxiao Shi19838042014-06-21 00:34:01 -0700135 static time::nanoseconds
Davide Pesavento50a6af32019-02-21 00:04:40 -0500136 getInitialLifetime()
137 {
138 return 4_s;
139 }
Junxiao Shi19838042014-06-21 00:34:01 -0700140
Davide Pesavento50a6af32019-02-21 00:04:40 -0500141 /** \brief Extend lifetime of an entry
Junxiao Shi65d00722014-02-17 10:50:20 -0700142 *
143 * The entry will be kept until at least now()+lifetime.
144 */
145 void
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000146 extendLifetime(Entry& entry, const time::nanoseconds& lifetime);
HangZhangc85a23c2014-03-01 15:55:55 +0800147
148 size_t
Davide Pesavento50a6af32019-02-21 00:04:40 -0500149 size() const
150 {
151 return m_nItems;
152 }
Junxiao Shi65d00722014-02-17 10:50:20 -0700153
154private:
155 void
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000156 cleanup(Entry& entry);
Junxiao Shi65d00722014-02-17 10:50:20 -0700157
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000158 Entry&
Junxiao Shie368d992014-12-02 23:44:31 -0700159 get(name_tree::Entry& nte);
HangZhangcb4fc832014-03-11 16:57:11 +0800160
Junxiao Shi057d1492018-03-20 17:14:18 +0000161 /** \tparam K a parameter acceptable to \c NameTree::findLongestPrefixMatch
Junxiao Shi767cb332015-01-08 09:35:49 -0700162 */
163 template<typename K>
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000164 Entry*
165 findLongestPrefixMatchImpl(const K& key, const EntryPredicate& pred) const;
Junxiao Shi767cb332015-01-08 09:35:49 -0700166
Junxiao Shi65d00722014-02-17 10:50:20 -0700167private:
HangZhangc85a23c2014-03-01 15:55:55 +0800168 NameTree& m_nameTree;
Davide Pesavento50a6af32019-02-21 00:04:40 -0500169 size_t m_nItems = 0;
Junxiao Shi65d00722014-02-17 10:50:20 -0700170};
171
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000172} // namespace measurements
173
174using measurements::Measurements;
175
Junxiao Shi65d00722014-02-17 10:50:20 -0700176} // namespace nfd
177
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700178#endif // NFD_DAEMON_TABLE_MEASUREMENTS_HPP