blob: 73b7ccafed72225ff915d0e46de82930875e8b3d [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#include "asf-measurements.hpp"
Davide Pesavento2cae8ca2019-04-18 20:48:05 -040027#include "common/global.hpp"
Vince Lehman8a4c29e2016-07-11 08:49:35 +000028
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040029namespace nfd::fw::asf {
Vince Lehman8a4c29e2016-07-11 08:49:35 +000030
Davide Pesaventoa6f637a2019-08-28 23:23:20 -040031time::nanoseconds
Davide Pesavento2c9d2ca2024-01-27 16:36:51 -050032FaceInfo::scheduleTimeout(const Name& interestName, ndn::scheduler::EventCallback cb)
Vince Lehman8a4c29e2016-07-11 08:49:35 +000033{
Davide Pesaventoa6f637a2019-08-28 23:23:20 -040034 BOOST_ASSERT(!m_timeoutEvent);
35 m_lastInterestName = interestName;
36 m_timeoutEvent = getScheduler().schedule(m_rttEstimator.getEstimatedRto(), std::move(cb));
37 return m_rttEstimator.getEstimatedRto();
Vince Lehman8a4c29e2016-07-11 08:49:35 +000038}
39
40void
Davide Pesaventoa6f637a2019-08-28 23:23:20 -040041FaceInfo::cancelTimeout(const Name& prefix)
Vince Lehman8a4c29e2016-07-11 08:49:35 +000042{
Davide Pesaventoa6f637a2019-08-28 23:23:20 -040043 if (m_lastInterestName.isPrefixOf(prefix)) {
44 m_timeoutEvent.cancel();
45 }
Vince Lehman8a4c29e2016-07-11 08:49:35 +000046}
47
48////////////////////////////////////////////////////////////////////////////////
49////////////////////////////////////////////////////////////////////////////////
50
Vince Lehman8a4c29e2016-07-11 08:49:35 +000051FaceInfo*
Davide Pesaventoa6f637a2019-08-28 23:23:20 -040052NamespaceInfo::getFaceInfo(FaceId faceId)
Vince Lehman8a4c29e2016-07-11 08:49:35 +000053{
Davide Pesaventoa6f637a2019-08-28 23:23:20 -040054 auto it = m_fiMap.find(faceId);
55 return it != m_fiMap.end() ? &it->second : nullptr;
Vince Lehman8a4c29e2016-07-11 08:49:35 +000056}
57
58FaceInfo&
Davide Pesaventoa6f637a2019-08-28 23:23:20 -040059NamespaceInfo::getOrCreateFaceInfo(FaceId faceId)
Vince Lehman8a4c29e2016-07-11 08:49:35 +000060{
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040061 auto [it, isNew] = m_fiMap.try_emplace(faceId, m_rttEstimatorOpts);
62 auto& faceInfo = it->second;
63 if (isNew) {
Davide Pesaventoa6f637a2019-08-28 23:23:20 -040064 extendFaceInfoLifetime(faceInfo, faceId);
Vince Lehman8a4c29e2016-07-11 08:49:35 +000065 }
Davide Pesaventoa6f637a2019-08-28 23:23:20 -040066 return faceInfo;
Vince Lehman8a4c29e2016-07-11 08:49:35 +000067}
68
69void
Ashlesh Gawande92e4ea52017-07-19 11:38:12 -050070NamespaceInfo::extendFaceInfoLifetime(FaceInfo& info, FaceId faceId)
Vince Lehman8a4c29e2016-07-11 08:49:35 +000071{
awlaneee97f532024-09-19 15:48:21 -050072 info.m_measurementExpiration = getScheduler().schedule(m_measurementLifetime,
Davide Pesaventoa6f637a2019-08-28 23:23:20 -040073 [=] { m_fiMap.erase(faceId); });
Vince Lehman8a4c29e2016-07-11 08:49:35 +000074}
75
76////////////////////////////////////////////////////////////////////////////////
77////////////////////////////////////////////////////////////////////////////////
78
Vince Lehman8a4c29e2016-07-11 08:49:35 +000079AsfMeasurements::AsfMeasurements(MeasurementsAccessor& measurements)
80 : m_measurements(measurements)
Davide Pesaventoa6f637a2019-08-28 23:23:20 -040081 , m_rttEstimatorOpts(make_shared<ndn::util::RttEstimator::Options>())
Vince Lehman8a4c29e2016-07-11 08:49:35 +000082{
83}
84
85FaceInfo*
Saurab Dulal432be572021-01-26 12:09:29 -060086AsfMeasurements::getFaceInfo(const fib::Entry& fibEntry, const Name& interestName, FaceId faceId)
Vince Lehman8a4c29e2016-07-11 08:49:35 +000087{
Saurab Dulal432be572021-01-26 12:09:29 -060088 return getOrCreateNamespaceInfo(fibEntry, interestName).getFaceInfo(faceId);
Vince Lehman8a4c29e2016-07-11 08:49:35 +000089}
90
91FaceInfo&
Saurab Dulal432be572021-01-26 12:09:29 -060092AsfMeasurements::getOrCreateFaceInfo(const fib::Entry& fibEntry, const Name& interestName, FaceId faceId)
Vince Lehman8a4c29e2016-07-11 08:49:35 +000093{
Saurab Dulal432be572021-01-26 12:09:29 -060094 return getOrCreateNamespaceInfo(fibEntry, interestName).getOrCreateFaceInfo(faceId);
Vince Lehman8a4c29e2016-07-11 08:49:35 +000095}
96
Junxiao Shifc021862016-08-25 21:51:18 +000097NamespaceInfo*
98AsfMeasurements::getNamespaceInfo(const Name& prefix)
Vince Lehman8a4c29e2016-07-11 08:49:35 +000099{
Saurab Dulal432be572021-01-26 12:09:29 -0600100 auto* me = m_measurements.findLongestPrefixMatch(prefix);
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000101 if (me == nullptr) {
102 return nullptr;
103 }
104
105 // Set or update entry lifetime
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000106 extendLifetime(*me);
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000107
awlaneee97f532024-09-19 15:48:21 -0500108 NamespaceInfo* info = me->insertStrategyInfo<NamespaceInfo>(m_rttEstimatorOpts, m_measurementsLifetime).first;
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000109 BOOST_ASSERT(info != nullptr);
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000110 return info;
111}
112
113NamespaceInfo&
Saurab Dulal432be572021-01-26 12:09:29 -0600114AsfMeasurements::getOrCreateNamespaceInfo(const fib::Entry& fibEntry, const Name& prefix)
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000115{
Saurab Dulal432be572021-01-26 12:09:29 -0600116 auto* me = m_measurements.get(fibEntry);
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000117
118 // If the FIB entry is not under the strategy's namespace, find a part of the prefix
119 // that falls under the strategy's namespace
120 for (size_t prefixLen = fibEntry.getPrefix().size() + 1;
Saurab Dulal432be572021-01-26 12:09:29 -0600121 me == nullptr && prefixLen <= prefix.size();
122 ++prefixLen) {
123 me = m_measurements.get(prefix.getPrefix(prefixLen));
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000124 }
125
126 // Either the FIB entry or the Interest's name must be under this strategy's namespace
127 BOOST_ASSERT(me != nullptr);
128
129 // Set or update entry lifetime
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000130 extendLifetime(*me);
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000131
awlaneee97f532024-09-19 15:48:21 -0500132 NamespaceInfo* info = me->insertStrategyInfo<NamespaceInfo>(m_rttEstimatorOpts, m_measurementsLifetime).first;
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000133 BOOST_ASSERT(info != nullptr);
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000134 return *info;
135}
136
137void
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000138AsfMeasurements::extendLifetime(measurements::Entry& me)
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000139{
awlaneee97f532024-09-19 15:48:21 -0500140 m_measurements.extendLifetime(me, m_measurementsLifetime);
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000141}
142
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400143} // namespace nfd::fw::asf