blob: 0e7ab48970310878dcd1e70115b7bc8f0dffe0df [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 Pesavento19779d82019-02-14 13:40:04 -05003 * 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#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*
Ashlesh Gawande92e4ea52017-07-19 11:38:12 -050095AsfMeasurements::getFaceInfo(const fib::Entry& fibEntry, const Interest& interest, FaceId faceId)
Vince Lehman8a4c29e2016-07-11 08:49:35 +000096{
Davide Pesaventoa6f637a2019-08-28 23:23:20 -040097 return getOrCreateNamespaceInfo(fibEntry, interest).getFaceInfo(faceId);
Vince Lehman8a4c29e2016-07-11 08:49:35 +000098}
99
100FaceInfo&
Ashlesh Gawande92e4ea52017-07-19 11:38:12 -0500101AsfMeasurements::getOrCreateFaceInfo(const fib::Entry& fibEntry, const Interest& interest,
102 FaceId faceId)
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000103{
Davide Pesaventoa6f637a2019-08-28 23:23:20 -0400104 return getOrCreateNamespaceInfo(fibEntry, interest).getOrCreateFaceInfo(faceId);
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000105}
106
Junxiao Shifc021862016-08-25 21:51:18 +0000107NamespaceInfo*
108AsfMeasurements::getNamespaceInfo(const Name& prefix)
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000109{
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000110 measurements::Entry* me = m_measurements.findLongestPrefixMatch(prefix);
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000111 if (me == nullptr) {
112 return nullptr;
113 }
114
115 // Set or update entry lifetime
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000116 extendLifetime(*me);
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000117
Davide Pesaventoa6f637a2019-08-28 23:23:20 -0400118 NamespaceInfo* info = me->insertStrategyInfo<NamespaceInfo>(m_rttEstimatorOpts).first;
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000119 BOOST_ASSERT(info != nullptr);
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000120 return info;
121}
122
123NamespaceInfo&
Junxiao Shifc021862016-08-25 21:51:18 +0000124AsfMeasurements::getOrCreateNamespaceInfo(const fib::Entry& fibEntry, const Interest& interest)
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000125{
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000126 measurements::Entry* me = m_measurements.get(fibEntry);
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000127
128 // If the FIB entry is not under the strategy's namespace, find a part of the prefix
129 // that falls under the strategy's namespace
130 for (size_t prefixLen = fibEntry.getPrefix().size() + 1;
131 me == nullptr && prefixLen <= interest.getName().size(); ++prefixLen) {
132 me = m_measurements.get(interest.getName().getPrefix(prefixLen));
133 }
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