blob: 77925b610df7f55a660a981100936d5c35049ae0 [file] [log] [blame]
Vince Lehman8a4c29e2016-07-11 08:49:35 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Ashlesh Gawande92e4ea52017-07-19 11:38:12 -05002/*
Davide Pesavento2c9d2ca2024-01-27 16:36:51 -05003 * Copyright (c) 2014-2024, Regents of the University of California,
Vince Lehman8a4c29e2016-07-11 08:49:35 +00004 * Arizona Board of Regents,
5 * Colorado State University,
6 * University Pierre & Marie Curie, Sorbonne University,
7 * Washington University in St. Louis,
8 * Beijing Institute of Technology,
9 * The University of Memphis.
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/>.
24 */
25
26#ifndef NFD_DAEMON_FW_ASF_MEASUREMENTS_HPP
27#define NFD_DAEMON_FW_ASF_MEASUREMENTS_HPP
28
Davide Pesavento972de802024-02-16 18:42:55 -050029#include "face/face-common.hpp"
Vince Lehman8a4c29e2016-07-11 08:49:35 +000030#include "fw/strategy-info.hpp"
31#include "table/measurements-accessor.hpp"
32
Ernest McCracken1402fa12019-06-09 00:36:28 -070033#include <ndn-cxx/util/rtt-estimator.hpp>
34
Davide Pesaventob7bfcb92022-05-22 23:55:23 -040035#include <unordered_map>
36
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040037namespace nfd::fw::asf {
Vince Lehman8a4c29e2016-07-11 08:49:35 +000038
awlane5fdcbec2023-12-15 14:56:05 -060039/**
40 * \brief Strategy information for each face in a namespace.
41 */
Vince Lehman8a4c29e2016-07-11 08:49:35 +000042class FaceInfo
43{
44public:
Davide Pesaventoa6f637a2019-08-28 23:23:20 -040045 explicit
46 FaceInfo(shared_ptr<const ndn::util::RttEstimator::Options> opts)
47 : m_rttEstimator(std::move(opts))
Vince Lehman8a4c29e2016-07-11 08:49:35 +000048 {
Vince Lehman8a4c29e2016-07-11 08:49:35 +000049 }
50
Vince Lehman8a4c29e2016-07-11 08:49:35 +000051 bool
52 isTimeoutScheduled() const
53 {
Davide Pesaventoa6f637a2019-08-28 23:23:20 -040054 return !!m_timeoutEvent;
55 }
56
57 time::nanoseconds
Davide Pesavento2c9d2ca2024-01-27 16:36:51 -050058 scheduleTimeout(const Name& interestName, ndn::scheduler::EventCallback cb);
Davide Pesaventoa6f637a2019-08-28 23:23:20 -040059
60 void
61 cancelTimeout(const Name& prefix);
62
63 void
64 recordRtt(time::nanoseconds rtt)
65 {
66 m_lastRtt = rtt;
67 m_rttEstimator.addMeasurement(rtt);
Vince Lehman8a4c29e2016-07-11 08:49:35 +000068 }
69
70 void
Davide Pesaventoa6f637a2019-08-28 23:23:20 -040071 recordTimeout(const Name& interestName)
72 {
73 m_lastRtt = RTT_TIMEOUT;
74 cancelTimeout(interestName);
75 }
Vince Lehman8a4c29e2016-07-11 08:49:35 +000076
Ernest McCracken1402fa12019-06-09 00:36:28 -070077 time::nanoseconds
Davide Pesaventoa6f637a2019-08-28 23:23:20 -040078 getLastRtt() const
Vince Lehman8a4c29e2016-07-11 08:49:35 +000079 {
Davide Pesaventoa6f637a2019-08-28 23:23:20 -040080 return m_lastRtt;
Vince Lehman8a4c29e2016-07-11 08:49:35 +000081 }
82
Ernest McCracken1402fa12019-06-09 00:36:28 -070083 time::nanoseconds
Vince Lehman8a4c29e2016-07-11 08:49:35 +000084 getSrtt() const
85 {
Davide Pesaventoa6f637a2019-08-28 23:23:20 -040086 return m_rttEstimator.getSmoothedRtt();
Vince Lehman8a4c29e2016-07-11 08:49:35 +000087 }
88
Ashlesh Gawande92e4ea52017-07-19 11:38:12 -050089 size_t
Saurab Dulalaf3ff5a2021-09-19 19:45:07 -040090 getNTimeouts() const
Ashlesh Gawande92e4ea52017-07-19 11:38:12 -050091 {
Saurab Dulalaf3ff5a2021-09-19 19:45:07 -040092 return m_nTimeouts;
Ashlesh Gawande92e4ea52017-07-19 11:38:12 -050093 }
94
95 void
Saurab Dulalaf3ff5a2021-09-19 19:45:07 -040096 setNTimeouts(size_t nTimeouts)
Ashlesh Gawande92e4ea52017-07-19 11:38:12 -050097 {
Saurab Dulalaf3ff5a2021-09-19 19:45:07 -040098 m_nTimeouts = nTimeouts;
Ashlesh Gawande92e4ea52017-07-19 11:38:12 -050099 }
100
Davide Pesaventoa6f637a2019-08-28 23:23:20 -0400101public:
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400102 static constexpr time::nanoseconds RTT_NO_MEASUREMENT = -1_ns;
103 static constexpr time::nanoseconds RTT_TIMEOUT = -2_ns;
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000104
105private:
Davide Pesaventoa6f637a2019-08-28 23:23:20 -0400106 ndn::util::RttEstimator m_rttEstimator;
107 time::nanoseconds m_lastRtt = RTT_NO_MEASUREMENT;
Junxiao Shifc021862016-08-25 21:51:18 +0000108 Name m_lastInterestName;
Saurab Dulalaf3ff5a2021-09-19 19:45:07 -0400109 size_t m_nTimeouts = 0;
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000110
111 // Timeout associated with measurement
Davide Pesavento2c9d2ca2024-01-27 16:36:51 -0500112 ndn::scheduler::ScopedEventId m_measurementExpiration;
Davide Pesaventoa6f637a2019-08-28 23:23:20 -0400113 friend class NamespaceInfo;
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000114
115 // RTO associated with Interest
Davide Pesavento2c9d2ca2024-01-27 16:36:51 -0500116 ndn::scheduler::ScopedEventId m_timeoutEvent;
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000117};
118
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000119////////////////////////////////////////////////////////////////////////////////
120////////////////////////////////////////////////////////////////////////////////
121
awlane5fdcbec2023-12-15 14:56:05 -0600122/**
123 * \brief Stores strategy information about each face in this namespace.
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000124 */
Davide Pesavento3db98072021-03-09 23:03:27 -0500125class NamespaceInfo final : public StrategyInfo
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000126{
127public:
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000128 static constexpr int
129 getTypeId()
130 {
131 return 1030;
132 }
133
Davide Pesaventoa6f637a2019-08-28 23:23:20 -0400134 explicit
awlaneee97f532024-09-19 15:48:21 -0500135 NamespaceInfo(shared_ptr<const ndn::util::RttEstimator::Options> opts, time::milliseconds measurementLifetime)
Davide Pesaventoa6f637a2019-08-28 23:23:20 -0400136 : m_rttEstimatorOpts(std::move(opts))
awlaneee97f532024-09-19 15:48:21 -0500137 , m_measurementLifetime(measurementLifetime)
Davide Pesaventoa6f637a2019-08-28 23:23:20 -0400138 {
139 }
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000140
141 FaceInfo*
Davide Pesaventoa6f637a2019-08-28 23:23:20 -0400142 getFaceInfo(FaceId faceId);
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000143
Davide Pesaventoa6f637a2019-08-28 23:23:20 -0400144 FaceInfo&
145 getOrCreateFaceInfo(FaceId faceId);
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000146
147 void
Ashlesh Gawande92e4ea52017-07-19 11:38:12 -0500148 extendFaceInfoLifetime(FaceInfo& info, FaceId faceId);
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000149
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000150 bool
151 isProbingDue() const
152 {
153 return m_isProbingDue;
154 }
155
156 void
157 setIsProbingDue(bool isProbingDue)
158 {
159 m_isProbingDue = isProbingDue;
160 }
161
162 bool
163 isFirstProbeScheduled() const
164 {
Davide Pesaventoa6f637a2019-08-28 23:23:20 -0400165 return m_isFirstProbeScheduled;
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000166 }
167
168 void
Davide Pesaventoa6f637a2019-08-28 23:23:20 -0400169 setIsFirstProbeScheduled(bool isScheduled)
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000170 {
Davide Pesaventoa6f637a2019-08-28 23:23:20 -0400171 m_isFirstProbeScheduled = isScheduled;
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000172 }
173
174private:
Davide Pesaventoa6f637a2019-08-28 23:23:20 -0400175 std::unordered_map<FaceId, FaceInfo> m_fiMap;
176 shared_ptr<const ndn::util::RttEstimator::Options> m_rttEstimatorOpts;
awlaneee97f532024-09-19 15:48:21 -0500177 time::milliseconds m_measurementLifetime;
Davide Pesaventoa6f637a2019-08-28 23:23:20 -0400178 bool m_isProbingDue = false;
179 bool m_isFirstProbeScheduled = false;
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000180};
181
182////////////////////////////////////////////////////////////////////////////////
183////////////////////////////////////////////////////////////////////////////////
184
awlane5fdcbec2023-12-15 14:56:05 -0600185/**
186 * \brief Helper class to retrieve and create strategy measurements.
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000187 */
188class AsfMeasurements : noncopyable
189{
190public:
191 explicit
192 AsfMeasurements(MeasurementsAccessor& measurements);
193
194 FaceInfo*
Saurab Dulal432be572021-01-26 12:09:29 -0600195 getFaceInfo(const fib::Entry& fibEntry, const Name& interestName, FaceId faceId);
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000196
197 FaceInfo&
Saurab Dulal432be572021-01-26 12:09:29 -0600198 getOrCreateFaceInfo(const fib::Entry& fibEntry, const Name& interestName, FaceId faceId);
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000199
Junxiao Shifc021862016-08-25 21:51:18 +0000200 NamespaceInfo*
201 getNamespaceInfo(const Name& prefix);
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000202
203 NamespaceInfo&
Saurab Dulal432be572021-01-26 12:09:29 -0600204 getOrCreateNamespaceInfo(const fib::Entry& fibEntry, const Name& prefix);
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000205
awlaneee97f532024-09-19 15:48:21 -0500206 void
207 setMeasurementsLifetime(time::milliseconds measurementsLifetime)
208 {
209 // Measurement lifetime should not expire as soon as it is configured
210 BOOST_ASSERT(measurementsLifetime > 0_ms);
211 m_measurementsLifetime = measurementsLifetime;
212 }
213
214 time::milliseconds
215 getMeasurementsLifetime() const
216 {
217 return m_measurementsLifetime;
218 }
219
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000220private:
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000221 void
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000222 extendLifetime(measurements::Entry& me);
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000223
224public:
awlaneee97f532024-09-19 15:48:21 -0500225 static constexpr time::milliseconds DEFAULT_MEASUREMENTS_LIFETIME = 5_min;
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000226
227private:
awlaneee97f532024-09-19 15:48:21 -0500228 time::milliseconds m_measurementsLifetime = DEFAULT_MEASUREMENTS_LIFETIME;
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000229 MeasurementsAccessor& m_measurements;
Davide Pesaventoa6f637a2019-08-28 23:23:20 -0400230 shared_ptr<const ndn::util::RttEstimator::Options> m_rttEstimatorOpts;
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000231};
232
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400233} // namespace nfd::fw::asf
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000234
235#endif // NFD_DAEMON_FW_ASF_MEASUREMENTS_HPP