blob: 2eb81ddd06d185196d2e7b77f1f7bcb5d876d9c0 [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/*
Saurab Dulal432be572021-01-26 12:09:29 -06003 * Copyright (c) 2014-2021, 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 -040033const time::nanoseconds FaceInfo::RTT_NO_MEASUREMENT{-1};
34const time::nanoseconds FaceInfo::RTT_TIMEOUT{-2};
Vince Lehman8a4c29e2016-07-11 08:49:35 +000035
Davide Pesaventoa6f637a2019-08-28 23:23:20 -040036time::nanoseconds
37FaceInfo::scheduleTimeout(const Name& interestName, scheduler::EventCallback cb)
Vince Lehman8a4c29e2016-07-11 08:49:35 +000038{
Davide Pesaventoa6f637a2019-08-28 23:23:20 -040039 BOOST_ASSERT(!m_timeoutEvent);
40 m_lastInterestName = interestName;
41 m_timeoutEvent = getScheduler().schedule(m_rttEstimator.getEstimatedRto(), std::move(cb));
42 return m_rttEstimator.getEstimatedRto();
Vince Lehman8a4c29e2016-07-11 08:49:35 +000043}
44
45void
Davide Pesaventoa6f637a2019-08-28 23:23:20 -040046FaceInfo::cancelTimeout(const Name& prefix)
Vince Lehman8a4c29e2016-07-11 08:49:35 +000047{
Davide Pesaventoa6f637a2019-08-28 23:23:20 -040048 if (m_lastInterestName.isPrefixOf(prefix)) {
49 m_timeoutEvent.cancel();
50 }
Vince Lehman8a4c29e2016-07-11 08:49:35 +000051}
52
53////////////////////////////////////////////////////////////////////////////////
54////////////////////////////////////////////////////////////////////////////////
55
Vince Lehman8a4c29e2016-07-11 08:49:35 +000056FaceInfo*
Davide Pesaventoa6f637a2019-08-28 23:23:20 -040057NamespaceInfo::getFaceInfo(FaceId faceId)
Vince Lehman8a4c29e2016-07-11 08:49:35 +000058{
Davide Pesaventoa6f637a2019-08-28 23:23:20 -040059 auto it = m_fiMap.find(faceId);
60 return it != m_fiMap.end() ? &it->second : nullptr;
Vince Lehman8a4c29e2016-07-11 08:49:35 +000061}
62
63FaceInfo&
Davide Pesaventoa6f637a2019-08-28 23:23:20 -040064NamespaceInfo::getOrCreateFaceInfo(FaceId faceId)
Vince Lehman8a4c29e2016-07-11 08:49:35 +000065{
Davide Pesaventoa6f637a2019-08-28 23:23:20 -040066 auto ret = m_fiMap.emplace(std::piecewise_construct,
67 std::forward_as_tuple(faceId),
68 std::forward_as_tuple(m_rttEstimatorOpts));
69 auto& faceInfo = ret.first->second;
70 if (ret.second) {
71 extendFaceInfoLifetime(faceInfo, faceId);
Vince Lehman8a4c29e2016-07-11 08:49:35 +000072 }
Davide Pesaventoa6f637a2019-08-28 23:23:20 -040073 return faceInfo;
Vince Lehman8a4c29e2016-07-11 08:49:35 +000074}
75
76void
Ashlesh Gawande92e4ea52017-07-19 11:38:12 -050077NamespaceInfo::extendFaceInfoLifetime(FaceInfo& info, FaceId faceId)
Vince Lehman8a4c29e2016-07-11 08:49:35 +000078{
Davide Pesaventoa6f637a2019-08-28 23:23:20 -040079 info.m_measurementExpiration = getScheduler().schedule(AsfMeasurements::MEASUREMENTS_LIFETIME,
80 [=] { m_fiMap.erase(faceId); });
Vince Lehman8a4c29e2016-07-11 08:49:35 +000081}
82
83////////////////////////////////////////////////////////////////////////////////
84////////////////////////////////////////////////////////////////////////////////
85
86constexpr time::microseconds AsfMeasurements::MEASUREMENTS_LIFETIME;
87
88AsfMeasurements::AsfMeasurements(MeasurementsAccessor& measurements)
89 : m_measurements(measurements)
Davide Pesaventoa6f637a2019-08-28 23:23:20 -040090 , m_rttEstimatorOpts(make_shared<ndn::util::RttEstimator::Options>())
Vince Lehman8a4c29e2016-07-11 08:49:35 +000091{
92}
93
94FaceInfo*
Saurab Dulal432be572021-01-26 12:09:29 -060095AsfMeasurements::getFaceInfo(const fib::Entry& fibEntry, const Name& interestName, FaceId faceId)
Vince Lehman8a4c29e2016-07-11 08:49:35 +000096{
Saurab Dulal432be572021-01-26 12:09:29 -060097 return getOrCreateNamespaceInfo(fibEntry, interestName).getFaceInfo(faceId);
Vince Lehman8a4c29e2016-07-11 08:49:35 +000098}
99
100FaceInfo&
Saurab Dulal432be572021-01-26 12:09:29 -0600101AsfMeasurements::getOrCreateFaceInfo(const fib::Entry& fibEntry, const Name& interestName, FaceId faceId)
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000102{
Saurab Dulal432be572021-01-26 12:09:29 -0600103 return getOrCreateNamespaceInfo(fibEntry, interestName).getOrCreateFaceInfo(faceId);
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000104}
105
Junxiao Shifc021862016-08-25 21:51:18 +0000106NamespaceInfo*
107AsfMeasurements::getNamespaceInfo(const Name& prefix)
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000108{
Saurab Dulal432be572021-01-26 12:09:29 -0600109 auto* me = m_measurements.findLongestPrefixMatch(prefix);
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000110 if (me == nullptr) {
111 return nullptr;
112 }
113
114 // Set or update entry lifetime
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000115 extendLifetime(*me);
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000116
Davide Pesaventoa6f637a2019-08-28 23:23:20 -0400117 NamespaceInfo* info = me->insertStrategyInfo<NamespaceInfo>(m_rttEstimatorOpts).first;
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000118 BOOST_ASSERT(info != nullptr);
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000119 return info;
120}
121
122NamespaceInfo&
Saurab Dulal432be572021-01-26 12:09:29 -0600123AsfMeasurements::getOrCreateNamespaceInfo(const fib::Entry& fibEntry, const Name& prefix)
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000124{
Saurab Dulal432be572021-01-26 12:09:29 -0600125 auto* me = m_measurements.get(fibEntry);
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000126
127 // If the FIB entry is not under the strategy's namespace, find a part of the prefix
128 // that falls under the strategy's namespace
129 for (size_t prefixLen = fibEntry.getPrefix().size() + 1;
Saurab Dulal432be572021-01-26 12:09:29 -0600130 me == nullptr && prefixLen <= prefix.size();
131 ++prefixLen) {
132 me = m_measurements.get(prefix.getPrefix(prefixLen));
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000133 }
134
135 // Either the FIB entry or the Interest's name must be under this strategy's namespace
136 BOOST_ASSERT(me != nullptr);
137
138 // Set or update entry lifetime
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000139 extendLifetime(*me);
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000140
Davide Pesaventoa6f637a2019-08-28 23:23:20 -0400141 NamespaceInfo* info = me->insertStrategyInfo<NamespaceInfo>(m_rttEstimatorOpts).first;
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000142 BOOST_ASSERT(info != nullptr);
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000143 return *info;
144}
145
146void
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000147AsfMeasurements::extendLifetime(measurements::Entry& me)
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000148{
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000149 m_measurements.extendLifetime(me, MEASUREMENTS_LIFETIME);
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000150}
151
152} // namespace asf
153} // namespace fw
154} // namespace nfd