asf-measurements.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014-2024, Regents of the University of California,
4  * 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"
27 #include "common/global.hpp"
28 
29 namespace nfd::fw::asf {
30 
31 time::nanoseconds
32 FaceInfo::scheduleTimeout(const Name& interestName, ndn::scheduler::EventCallback cb)
33 {
34  BOOST_ASSERT(!m_timeoutEvent);
35  m_lastInterestName = interestName;
36  m_timeoutEvent = getScheduler().schedule(m_rttEstimator.getEstimatedRto(), std::move(cb));
37  return m_rttEstimator.getEstimatedRto();
38 }
39 
40 void
41 FaceInfo::cancelTimeout(const Name& prefix)
42 {
43  if (m_lastInterestName.isPrefixOf(prefix)) {
44  m_timeoutEvent.cancel();
45  }
46 }
47 
50 
51 FaceInfo*
53 {
54  auto it = m_fiMap.find(faceId);
55  return it != m_fiMap.end() ? &it->second : nullptr;
56 }
57 
58 FaceInfo&
60 {
61  auto [it, isNew] = m_fiMap.try_emplace(faceId, m_rttEstimatorOpts);
62  auto& faceInfo = it->second;
63  if (isNew) {
64  extendFaceInfoLifetime(faceInfo, faceId);
65  }
66  return faceInfo;
67 }
68 
69 void
71 {
72  info.m_measurementExpiration = getScheduler().schedule(AsfMeasurements::MEASUREMENTS_LIFETIME,
73  [=] { m_fiMap.erase(faceId); });
74 }
75 
78 
80  : m_measurements(measurements)
81  , m_rttEstimatorOpts(make_shared<ndn::util::RttEstimator::Options>())
82 {
83 }
84 
85 FaceInfo*
86 AsfMeasurements::getFaceInfo(const fib::Entry& fibEntry, const Name& interestName, FaceId faceId)
87 {
88  return getOrCreateNamespaceInfo(fibEntry, interestName).getFaceInfo(faceId);
89 }
90 
91 FaceInfo&
92 AsfMeasurements::getOrCreateFaceInfo(const fib::Entry& fibEntry, const Name& interestName, FaceId faceId)
93 {
94  return getOrCreateNamespaceInfo(fibEntry, interestName).getOrCreateFaceInfo(faceId);
95 }
96 
99 {
100  auto* me = m_measurements.findLongestPrefixMatch(prefix);
101  if (me == nullptr) {
102  return nullptr;
103  }
104 
105  // Set or update entry lifetime
106  extendLifetime(*me);
107 
108  NamespaceInfo* info = me->insertStrategyInfo<NamespaceInfo>(m_rttEstimatorOpts).first;
109  BOOST_ASSERT(info != nullptr);
110  return info;
111 }
112 
114 AsfMeasurements::getOrCreateNamespaceInfo(const fib::Entry& fibEntry, const Name& prefix)
115 {
116  auto* me = m_measurements.get(fibEntry);
117 
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;
121  me == nullptr && prefixLen <= prefix.size();
122  ++prefixLen) {
123  me = m_measurements.get(prefix.getPrefix(prefixLen));
124  }
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
130  extendLifetime(*me);
131 
132  NamespaceInfo* info = me->insertStrategyInfo<NamespaceInfo>(m_rttEstimatorOpts).first;
133  BOOST_ASSERT(info != nullptr);
134  return *info;
135 }
136 
137 void
138 AsfMeasurements::extendLifetime(measurements::Entry& me)
139 {
140  m_measurements.extendLifetime(me, MEASUREMENTS_LIFETIME);
141 }
142 
143 } // namespace nfd::fw::asf
Represents an entry in the FIB.
Definition: fib-entry.hpp:91
const Name & getPrefix() const noexcept
Definition: fib-entry.hpp:97
NamespaceInfo & getOrCreateNamespaceInfo(const fib::Entry &fibEntry, const Name &prefix)
AsfMeasurements(MeasurementsAccessor &measurements)
static constexpr time::microseconds MEASUREMENTS_LIFETIME
FaceInfo & getOrCreateFaceInfo(const fib::Entry &fibEntry, const Name &interestName, FaceId faceId)
FaceInfo * getFaceInfo(const fib::Entry &fibEntry, const Name &interestName, FaceId faceId)
NamespaceInfo * getNamespaceInfo(const Name &prefix)
Strategy information for each face in a namespace.
void cancelTimeout(const Name &prefix)
time::nanoseconds scheduleTimeout(const Name &interestName, ndn::scheduler::EventCallback cb)
Stores strategy information about each face in this namespace.
FaceInfo * getFaceInfo(FaceId faceId)
void extendFaceInfoLifetime(FaceInfo &info, FaceId faceId)
FaceInfo & getOrCreateFaceInfo(FaceId faceId)
Represents an entry in the Measurements table.
Allows fw::Strategy to access the portion of Measurements table under its namespace.
void extendLifetime(Entry &entry, const time::nanoseconds &lifetime)
Extend lifetime of an entry.
Entry * findLongestPrefixMatch(const Name &name, const EntryPredicate &pred=AnyEntry()) const
Perform a longest prefix match for name.
Entry * get(const Name &name)
Find or insert a Measurements entry for name.
Definition: dns-srv.cpp:44
uint64_t FaceId
Identifies a face.
Definition: face-common.hpp:48
ndn::Scheduler & getScheduler()
Returns the global Scheduler instance for the calling thread.
Definition: global.cpp:45