blob: 4100fcfbe807029f534e717869ae05be550cb264 [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 Pesavento2c9d2ca2024-01-27 16:36:51 -05003 * Copyright (c) 2014-2024, 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
Davide Pesavento2c9d2ca2024-01-27 16:36:51 -050032#include <functional>
33
Junxiao Shi65d00722014-02-17 10:50:20 -070034namespace nfd {
35
HangZhangc85a23c2014-03-01 15:55:55 +080036namespace fib {
37class Entry;
Junxiao Shib30c7b02015-01-07 15:45:54 -070038} // namespace fib
HangZhangc85a23c2014-03-01 15:55:55 +080039
40namespace pit {
41class Entry;
Junxiao Shib30c7b02015-01-07 15:45:54 -070042} // namespace pit
HangZhangc85a23c2014-03-01 15:55:55 +080043
Junxiao Shib30c7b02015-01-07 15:45:54 -070044namespace measurements {
HangZhangc85a23c2014-03-01 15:55:55 +080045
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040046/**
47 * \brief A predicate that accepts or rejects an entry.
Junxiao Shib30c7b02015-01-07 15:45:54 -070048 */
Davide Pesavento50a6af32019-02-21 00:04:40 -050049using EntryPredicate = std::function<bool(const Entry&)>;
Junxiao Shib30c7b02015-01-07 15:45:54 -070050
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040051/**
52 * \brief An #EntryPredicate that accepts any entry.
Junxiao Shib30c7b02015-01-07 15:45:54 -070053 */
54class AnyEntry
55{
56public:
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040057 constexpr bool
58 operator()(const Entry&) const noexcept
Junxiao Shib30c7b02015-01-07 15:45:54 -070059 {
60 return true;
61 }
62};
63
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040064/**
65 * \brief An #EntryPredicate that accepts an entry if it has StrategyInfo of type T.
Junxiao Shi80f9fcd2016-07-23 02:48:36 +000066 */
Junxiao Shib30c7b02015-01-07 15:45:54 -070067template<typename T>
68class EntryWithStrategyInfo
69{
70public:
71 bool
Junxiao Shi80f9fcd2016-07-23 02:48:36 +000072 operator()(const Entry& entry) const
Junxiao Shib30c7b02015-01-07 15:45:54 -070073 {
74 return entry.getStrategyInfo<T>() != nullptr;
75 }
76};
77
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040078/**
79 * \brief The %Measurements table.
Junxiao Shi057d1492018-03-20 17:14:18 +000080 *
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040081 * 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 Shi65d00722014-02-17 10:50:20 -070084 */
85class Measurements : noncopyable
86{
87public:
HangZhangc85a23c2014-03-01 15:55:55 +080088 explicit
Junxiao Shi057d1492018-03-20 17:14:18 +000089 Measurements(NameTree& nameTree);
Junxiao Shi65d00722014-02-17 10:50:20 -070090
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040091 /** \brief Maximum depth of a %Measurements entry.
Junxiao Shi057d1492018-03-20 17:14:18 +000092 */
93 static constexpr size_t
94 getMaxDepth()
95 {
96 return NameTree::getMaxDepth();
97 }
98
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040099 /** \brief Find or insert an entry by name.
Junxiao Shi057d1492018-03-20 17:14:18 +0000100 *
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400101 * 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 Shie368d992014-12-02 23:44:31 -0700103 */
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000104 Entry&
Junxiao Shi65d00722014-02-17 10:50:20 -0700105 get(const Name& name);
106
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400107 /** \brief Equivalent to `get(fibEntry.getPrefix())`.
Junxiao Shie368d992014-12-02 23:44:31 -0700108 */
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000109 Entry&
Junxiao Shi65d00722014-02-17 10:50:20 -0700110 get(const fib::Entry& fibEntry);
111
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400112 /** \brief Equivalent to `get(pitEntry.getName(), std::min(pitEntry.getName().size(), getMaxDepth()))`.
Junxiao Shie368d992014-12-02 23:44:31 -0700113 */
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000114 Entry&
Junxiao Shi65d00722014-02-17 10:50:20 -0700115 get(const pit::Entry& pitEntry);
116
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400117 /** \brief Find or insert a parent entry.
Junxiao Shi057d1492018-03-20 17:14:18 +0000118 * \retval nullptr child is the root entry
119 * \return get(child.getName().getPrefix(-1))
Junxiao Shi65d00722014-02-17 10:50:20 -0700120 */
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000121 Entry*
122 getParent(const Entry& child);
Junxiao Shi65d00722014-02-17 10:50:20 -0700123
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400124 /** \brief Perform a longest prefix match for \p name.
Junxiao Shie368d992014-12-02 23:44:31 -0700125 */
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000126 Entry*
Junxiao Shib30c7b02015-01-07 15:45:54 -0700127 findLongestPrefixMatch(const Name& name,
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000128 const EntryPredicate& pred = AnyEntry()) const;
Junxiao Shi65d00722014-02-17 10:50:20 -0700129
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400130 /** \brief Perform a longest prefix match for `pitEntry.getName()`.
Junxiao Shi767cb332015-01-08 09:35:49 -0700131 */
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000132 Entry*
Junxiao Shi767cb332015-01-08 09:35:49 -0700133 findLongestPrefixMatch(const pit::Entry& pitEntry,
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000134 const EntryPredicate& pred = AnyEntry()) const;
Junxiao Shi767cb332015-01-08 09:35:49 -0700135
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400136 /** \brief Perform an exact match.
Junxiao Shie368d992014-12-02 23:44:31 -0700137 */
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000138 Entry*
HangZhangc85a23c2014-03-01 15:55:55 +0800139 findExactMatch(const Name& name) const;
140
Junxiao Shi19838042014-06-21 00:34:01 -0700141 static time::nanoseconds
Davide Pesavento50a6af32019-02-21 00:04:40 -0500142 getInitialLifetime()
143 {
144 return 4_s;
145 }
Junxiao Shi19838042014-06-21 00:34:01 -0700146
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400147 /** \brief Extend lifetime of an entry.
Junxiao Shi65d00722014-02-17 10:50:20 -0700148 *
149 * The entry will be kept until at least now()+lifetime.
150 */
151 void
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000152 extendLifetime(Entry& entry, const time::nanoseconds& lifetime);
HangZhangc85a23c2014-03-01 15:55:55 +0800153
154 size_t
Davide Pesavento50a6af32019-02-21 00:04:40 -0500155 size() const
156 {
157 return m_nItems;
158 }
Junxiao Shi65d00722014-02-17 10:50:20 -0700159
160private:
161 void
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000162 cleanup(Entry& entry);
Junxiao Shi65d00722014-02-17 10:50:20 -0700163
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000164 Entry&
Junxiao Shie368d992014-12-02 23:44:31 -0700165 get(name_tree::Entry& nte);
HangZhangcb4fc832014-03-11 16:57:11 +0800166
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400167 /** \tparam K a parameter acceptable to NameTree::findLongestPrefixMatch()
Junxiao Shi767cb332015-01-08 09:35:49 -0700168 */
169 template<typename K>
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000170 Entry*
171 findLongestPrefixMatchImpl(const K& key, const EntryPredicate& pred) const;
Junxiao Shi767cb332015-01-08 09:35:49 -0700172
Junxiao Shi65d00722014-02-17 10:50:20 -0700173private:
HangZhangc85a23c2014-03-01 15:55:55 +0800174 NameTree& m_nameTree;
Davide Pesavento50a6af32019-02-21 00:04:40 -0500175 size_t m_nItems = 0;
Junxiao Shi65d00722014-02-17 10:50:20 -0700176};
177
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000178} // namespace measurements
179
180using measurements::Measurements;
181
Junxiao Shi65d00722014-02-17 10:50:20 -0700182} // namespace nfd
183
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700184#endif // NFD_DAEMON_TABLE_MEASUREMENTS_HPP