blob: c49f5d5436390c6a933af3ca74dab28f9040d295 [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 Pesaventoa3a7a4e2022-05-29 16:06:22 -04003 * Copyright (c) 2014-2022, 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#include "asf-measurements.hpp"
Davide Pesavento2cae8ca2019-04-18 20:48:05 -040027#include "common/global.hpp"
Vince Lehman8a4c29e2016-07-11 08:49:35 +000028
29namespace nfd {
30namespace fw {
31namespace asf {
32
Davide Pesaventoa6f637a2019-08-28 23:23:20 -040033time::nanoseconds
34FaceInfo::scheduleTimeout(const Name& interestName, scheduler::EventCallback cb)
Vince Lehman8a4c29e2016-07-11 08:49:35 +000035{
Davide Pesaventoa6f637a2019-08-28 23:23:20 -040036 BOOST_ASSERT(!m_timeoutEvent);
37 m_lastInterestName = interestName;
38 m_timeoutEvent = getScheduler().schedule(m_rttEstimator.getEstimatedRto(), std::move(cb));
39 return m_rttEstimator.getEstimatedRto();
Vince Lehman8a4c29e2016-07-11 08:49:35 +000040}
41
42void
Davide Pesaventoa6f637a2019-08-28 23:23:20 -040043FaceInfo::cancelTimeout(const Name& prefix)
Vince Lehman8a4c29e2016-07-11 08:49:35 +000044{
Davide Pesaventoa6f637a2019-08-28 23:23:20 -040045 if (m_lastInterestName.isPrefixOf(prefix)) {
46 m_timeoutEvent.cancel();
47 }
Vince Lehman8a4c29e2016-07-11 08:49:35 +000048}
49
50////////////////////////////////////////////////////////////////////////////////
51////////////////////////////////////////////////////////////////////////////////
52
Vince Lehman8a4c29e2016-07-11 08:49:35 +000053FaceInfo*
Davide Pesaventoa6f637a2019-08-28 23:23:20 -040054NamespaceInfo::getFaceInfo(FaceId faceId)
Vince Lehman8a4c29e2016-07-11 08:49:35 +000055{
Davide Pesaventoa6f637a2019-08-28 23:23:20 -040056 auto it = m_fiMap.find(faceId);
57 return it != m_fiMap.end() ? &it->second : nullptr;
Vince Lehman8a4c29e2016-07-11 08:49:35 +000058}
59
60FaceInfo&
Davide Pesaventoa6f637a2019-08-28 23:23:20 -040061NamespaceInfo::getOrCreateFaceInfo(FaceId faceId)
Vince Lehman8a4c29e2016-07-11 08:49:35 +000062{
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040063 auto [it, isNew] = m_fiMap.try_emplace(faceId, m_rttEstimatorOpts);
64 auto& faceInfo = it->second;
65 if (isNew) {
Davide Pesaventoa6f637a2019-08-28 23:23:20 -040066 extendFaceInfoLifetime(faceInfo, faceId);
Vince Lehman8a4c29e2016-07-11 08:49:35 +000067 }
Davide Pesaventoa6f637a2019-08-28 23:23:20 -040068 return faceInfo;
Vince Lehman8a4c29e2016-07-11 08:49:35 +000069}
70
71void
Ashlesh Gawande92e4ea52017-07-19 11:38:12 -050072NamespaceInfo::extendFaceInfoLifetime(FaceInfo& info, FaceId faceId)
Vince Lehman8a4c29e2016-07-11 08:49:35 +000073{
Davide Pesaventoa6f637a2019-08-28 23:23:20 -040074 info.m_measurementExpiration = getScheduler().schedule(AsfMeasurements::MEASUREMENTS_LIFETIME,
75 [=] { m_fiMap.erase(faceId); });
Vince Lehman8a4c29e2016-07-11 08:49:35 +000076}
77
78////////////////////////////////////////////////////////////////////////////////
79////////////////////////////////////////////////////////////////////////////////
80
Vince Lehman8a4c29e2016-07-11 08:49:35 +000081AsfMeasurements::AsfMeasurements(MeasurementsAccessor& measurements)
82 : m_measurements(measurements)
Davide Pesaventoa6f637a2019-08-28 23:23:20 -040083 , m_rttEstimatorOpts(make_shared<ndn::util::RttEstimator::Options>())
Vince Lehman8a4c29e2016-07-11 08:49:35 +000084{
85}
86
87FaceInfo*
Saurab Dulal432be572021-01-26 12:09:29 -060088AsfMeasurements::getFaceInfo(const fib::Entry& fibEntry, const Name& interestName, FaceId faceId)
Vince Lehman8a4c29e2016-07-11 08:49:35 +000089{
Saurab Dulal432be572021-01-26 12:09:29 -060090 return getOrCreateNamespaceInfo(fibEntry, interestName).getFaceInfo(faceId);
Vince Lehman8a4c29e2016-07-11 08:49:35 +000091}
92
93FaceInfo&
Saurab Dulal432be572021-01-26 12:09:29 -060094AsfMeasurements::getOrCreateFaceInfo(const fib::Entry& fibEntry, const Name& interestName, FaceId faceId)
Vince Lehman8a4c29e2016-07-11 08:49:35 +000095{
Saurab Dulal432be572021-01-26 12:09:29 -060096 return getOrCreateNamespaceInfo(fibEntry, interestName).getOrCreateFaceInfo(faceId);
Vince Lehman8a4c29e2016-07-11 08:49:35 +000097}
98
Junxiao Shifc021862016-08-25 21:51:18 +000099NamespaceInfo*
100AsfMeasurements::getNamespaceInfo(const Name& prefix)
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000101{
Saurab Dulal432be572021-01-26 12:09:29 -0600102 auto* me = m_measurements.findLongestPrefixMatch(prefix);
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000103 if (me == nullptr) {
104 return nullptr;
105 }
106
107 // Set or update entry lifetime
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000108 extendLifetime(*me);
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000109
Davide Pesaventoa6f637a2019-08-28 23:23:20 -0400110 NamespaceInfo* info = me->insertStrategyInfo<NamespaceInfo>(m_rttEstimatorOpts).first;
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000111 BOOST_ASSERT(info != nullptr);
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000112 return info;
113}
114
115NamespaceInfo&
Saurab Dulal432be572021-01-26 12:09:29 -0600116AsfMeasurements::getOrCreateNamespaceInfo(const fib::Entry& fibEntry, const Name& prefix)
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000117{
Saurab Dulal432be572021-01-26 12:09:29 -0600118 auto* me = m_measurements.get(fibEntry);
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000119
120 // If the FIB entry is not under the strategy's namespace, find a part of the prefix
121 // that falls under the strategy's namespace
122 for (size_t prefixLen = fibEntry.getPrefix().size() + 1;
Saurab Dulal432be572021-01-26 12:09:29 -0600123 me == nullptr && prefixLen <= prefix.size();
124 ++prefixLen) {
125 me = m_measurements.get(prefix.getPrefix(prefixLen));
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000126 }
127
128 // Either the FIB entry or the Interest's name must be under this strategy's namespace
129 BOOST_ASSERT(me != nullptr);
130
131 // Set or update entry lifetime
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000132 extendLifetime(*me);
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000133
Davide Pesaventoa6f637a2019-08-28 23:23:20 -0400134 NamespaceInfo* info = me->insertStrategyInfo<NamespaceInfo>(m_rttEstimatorOpts).first;
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000135 BOOST_ASSERT(info != nullptr);
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000136 return *info;
137}
138
139void
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000140AsfMeasurements::extendLifetime(measurements::Entry& me)
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000141{
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000142 m_measurements.extendLifetime(me, MEASUREMENTS_LIFETIME);
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000143}
144
145} // namespace asf
146} // namespace fw
147} // namespace nfd