blob: 0032f1420331bf0a9303a4a9b7e03ce6b0621d2a [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 Pesavento14e71f02019-03-28 17:35:25 -04003 * Copyright (c) 2014-2019, 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
Vince Lehman8a4c29e2016-07-11 08:49:35 +000029#include "fw/strategy-info.hpp"
30#include "table/measurements-accessor.hpp"
31
Ernest McCracken1402fa12019-06-09 00:36:28 -070032#include <ndn-cxx/util/rtt-estimator.hpp>
33
Vince Lehman8a4c29e2016-07-11 08:49:35 +000034namespace nfd {
35namespace fw {
36namespace asf {
37
38class RttStats
39{
40public:
Vince Lehman8a4c29e2016-07-11 08:49:35 +000041 RttStats();
42
43 void
Ernest McCracken1402fa12019-06-09 00:36:28 -070044 addRttMeasurement(time::nanoseconds rtt);
Vince Lehman8a4c29e2016-07-11 08:49:35 +000045
46 void
47 recordTimeout()
48 {
49 m_rtt = RTT_TIMEOUT;
50 }
51
Ernest McCracken1402fa12019-06-09 00:36:28 -070052 time::nanoseconds
Vince Lehman8a4c29e2016-07-11 08:49:35 +000053 getRtt() const
54 {
55 return m_rtt;
56 }
57
Ernest McCracken1402fa12019-06-09 00:36:28 -070058 time::nanoseconds
Vince Lehman8a4c29e2016-07-11 08:49:35 +000059 getSrtt() const
60 {
61 return m_srtt;
62 }
63
Ernest McCracken1402fa12019-06-09 00:36:28 -070064 time::nanoseconds
Vince Lehman8a4c29e2016-07-11 08:49:35 +000065 computeRto() const
66 {
Ernest McCracken1402fa12019-06-09 00:36:28 -070067 return m_rttEstimator.getEstimatedRto();
Vince Lehman8a4c29e2016-07-11 08:49:35 +000068 }
69
Vince Lehman8a4c29e2016-07-11 08:49:35 +000070public:
Ernest McCracken1402fa12019-06-09 00:36:28 -070071 static const time::nanoseconds RTT_TIMEOUT;
72 static const time::nanoseconds RTT_NO_MEASUREMENT;
Vince Lehman8a4c29e2016-07-11 08:49:35 +000073
74private:
Ernest McCracken1402fa12019-06-09 00:36:28 -070075 time::nanoseconds m_srtt;
76 time::nanoseconds m_rtt;
77 ndn::util::RttEstimator m_rttEstimator;
Vince Lehman8a4c29e2016-07-11 08:49:35 +000078
79 static const double ALPHA;
80};
81
82////////////////////////////////////////////////////////////////////////////////
83////////////////////////////////////////////////////////////////////////////////
84
85/** \brief Strategy information for each face in a namespace
86*/
87class FaceInfo
88{
89public:
90 class Error : public std::runtime_error
91 {
92 public:
93 explicit
94 Error(const std::string& what)
95 : std::runtime_error(what)
96 {
97 }
98 };
99
100 FaceInfo();
101
102 ~FaceInfo();
103
104 void
Junxiao Shifc021862016-08-25 21:51:18 +0000105 setTimeoutEvent(const scheduler::EventId& id, const Name& interestName);
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000106
107 void
108 setMeasurementExpirationEventId(const scheduler::EventId& id)
109 {
110 m_measurementExpirationId = id;
111 }
112
113 const scheduler::EventId&
114 getMeasurementExpirationEventId()
115 {
116 return m_measurementExpirationId;
117 }
118
119 void
Junxiao Shifc021862016-08-25 21:51:18 +0000120 cancelTimeoutEvent(const Name& prefix);
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000121
122 bool
123 isTimeoutScheduled() const
124 {
125 return m_isTimeoutScheduled;
126 }
127
128 void
Junxiao Shi15e98b02016-08-12 11:21:44 +0000129 recordRtt(const shared_ptr<pit::Entry>& pitEntry, const Face& inFace);
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000130
131 void
Junxiao Shifc021862016-08-25 21:51:18 +0000132 recordTimeout(const Name& interestName);
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000133
134 bool
135 isTimeout() const
136 {
137 return getRtt() == RttStats::RTT_TIMEOUT;
138 }
139
Ernest McCracken1402fa12019-06-09 00:36:28 -0700140 time::nanoseconds
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000141 computeRto() const
142 {
143 return m_rttStats.computeRto();
144 }
145
Ernest McCracken1402fa12019-06-09 00:36:28 -0700146 time::nanoseconds
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000147 getRtt() const
148 {
149 return m_rttStats.getRtt();
150 }
151
Ernest McCracken1402fa12019-06-09 00:36:28 -0700152 time::nanoseconds
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000153 getSrtt() const
154 {
155 return m_rttStats.getSrtt();
156 }
157
158 bool
159 hasSrttMeasurement() const
160 {
161 return getSrtt() != RttStats::RTT_NO_MEASUREMENT;
162 }
163
Ashlesh Gawande92e4ea52017-07-19 11:38:12 -0500164 size_t
165 getNSilentTimeouts() const
166 {
167 return m_nSilentTimeouts;
168 }
169
170 void
171 setNSilentTimeouts(size_t nSilentTimeouts)
172 {
173 m_nSilentTimeouts = nSilentTimeouts;
174 }
175
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000176private:
177 void
178 cancelTimeoutEvent();
179
180 bool
Junxiao Shifc021862016-08-25 21:51:18 +0000181 doesNameMatchLastInterest(const Name& name);
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000182
183private:
184 RttStats m_rttStats;
Junxiao Shifc021862016-08-25 21:51:18 +0000185 Name m_lastInterestName;
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000186
187 // Timeout associated with measurement
188 scheduler::EventId m_measurementExpirationId;
189
190 // RTO associated with Interest
191 scheduler::EventId m_timeoutEventId;
192 bool m_isTimeoutScheduled;
Ashlesh Gawande92e4ea52017-07-19 11:38:12 -0500193 size_t m_nSilentTimeouts;
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000194};
195
Ashlesh Gawande92e4ea52017-07-19 11:38:12 -0500196typedef std::unordered_map<FaceId, FaceInfo> FaceInfoTable;
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000197
198////////////////////////////////////////////////////////////////////////////////
199////////////////////////////////////////////////////////////////////////////////
200
Ernest McCracken1402fa12019-06-09 00:36:28 -0700201/** \brief stores strategy information about each face in this namespace
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000202 */
203class NamespaceInfo : public StrategyInfo
204{
205public:
206 NamespaceInfo();
207
208 static constexpr int
209 getTypeId()
210 {
211 return 1030;
212 }
213
214 FaceInfo&
Ashlesh Gawande92e4ea52017-07-19 11:38:12 -0500215 getOrCreateFaceInfo(const fib::Entry& fibEntry, FaceId faceId);
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000216
217 FaceInfo*
Ashlesh Gawande92e4ea52017-07-19 11:38:12 -0500218 getFaceInfo(const fib::Entry& fibEntry, FaceId faceId);
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000219
220 void
Ashlesh Gawande92e4ea52017-07-19 11:38:12 -0500221 expireFaceInfo(FaceId faceId);
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000222
223 void
Ashlesh Gawande92e4ea52017-07-19 11:38:12 -0500224 extendFaceInfoLifetime(FaceInfo& info, FaceId faceId);
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000225
Ashlesh Gawandecad76b62017-04-04 15:28:30 -0500226 FaceInfo*
Ashlesh Gawande92e4ea52017-07-19 11:38:12 -0500227 get(FaceId faceId)
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000228 {
Ashlesh Gawandecad76b62017-04-04 15:28:30 -0500229 if (m_fit.find(faceId) != m_fit.end()) {
230 return &m_fit.at(faceId);
231 }
232 else {
233 return nullptr;
234 }
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000235 }
236
237 FaceInfoTable::iterator
Ashlesh Gawande92e4ea52017-07-19 11:38:12 -0500238 find(FaceId faceId)
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000239 {
240 return m_fit.find(faceId);
241 }
242
243 FaceInfoTable::iterator
244 end()
245 {
246 return m_fit.end();
247 }
248
Ernest McCracken1402fa12019-06-09 00:36:28 -0700249 FaceInfoTable::iterator
Ashlesh Gawande92e4ea52017-07-19 11:38:12 -0500250 insert(FaceId faceId)
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000251 {
Ernest McCracken1402fa12019-06-09 00:36:28 -0700252 return m_fit.emplace(std::piecewise_construct,
253 std::forward_as_tuple(faceId),
254 std::forward_as_tuple()).first;
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000255 }
256
257 bool
258 isProbingDue() const
259 {
260 return m_isProbingDue;
261 }
262
263 void
264 setIsProbingDue(bool isProbingDue)
265 {
266 m_isProbingDue = isProbingDue;
267 }
268
269 bool
270 isFirstProbeScheduled() const
271 {
272 return m_hasFirstProbeBeenScheduled;
273 }
274
275 void
276 setHasFirstProbeBeenScheduled(bool hasBeenScheduled)
277 {
278 m_hasFirstProbeBeenScheduled = hasBeenScheduled;
279 }
280
281private:
282 FaceInfoTable m_fit;
283
284 bool m_isProbingDue;
285 bool m_hasFirstProbeBeenScheduled;
286};
287
288////////////////////////////////////////////////////////////////////////////////
289////////////////////////////////////////////////////////////////////////////////
290
291/** \brief Helper class to retrieve and create strategy measurements
292 */
293class AsfMeasurements : noncopyable
294{
295public:
296 explicit
297 AsfMeasurements(MeasurementsAccessor& measurements);
298
299 FaceInfo*
Ashlesh Gawande92e4ea52017-07-19 11:38:12 -0500300 getFaceInfo(const fib::Entry& fibEntry, const Interest& interest, FaceId faceId);
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000301
302 FaceInfo&
Ashlesh Gawande92e4ea52017-07-19 11:38:12 -0500303 getOrCreateFaceInfo(const fib::Entry& fibEntry, const Interest& interest, FaceId faceId);
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000304
Junxiao Shifc021862016-08-25 21:51:18 +0000305 NamespaceInfo*
306 getNamespaceInfo(const Name& prefix);
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000307
308 NamespaceInfo&
Junxiao Shifc021862016-08-25 21:51:18 +0000309 getOrCreateNamespaceInfo(const fib::Entry& fibEntry, const Interest& interest);
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000310
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000311private:
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000312 void
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000313 extendLifetime(measurements::Entry& me);
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000314
315public:
Davide Pesavento14e71f02019-03-28 17:35:25 -0400316 static constexpr time::microseconds MEASUREMENTS_LIFETIME = 5_min;
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000317
318private:
319 MeasurementsAccessor& m_measurements;
320};
321
322} // namespace asf
323} // namespace fw
324} // namespace nfd
325
326#endif // NFD_DAEMON_FW_ASF_MEASUREMENTS_HPP