Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Ashlesh Gawande | 92e4ea5 | 2017-07-19 11:38:12 -0500 | [diff] [blame] | 2 | /* |
Saurab Dulal | 432be57 | 2021-01-26 12:09:29 -0600 | [diff] [blame^] | 3 | * Copyright (c) 2014-2021, Regents of the University of California, |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 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" |
Davide Pesavento | 2cae8ca | 2019-04-18 20:48:05 -0400 | [diff] [blame] | 27 | #include "common/global.hpp" |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 28 | |
| 29 | namespace nfd { |
| 30 | namespace fw { |
| 31 | namespace asf { |
| 32 | |
Davide Pesavento | a6f637a | 2019-08-28 23:23:20 -0400 | [diff] [blame] | 33 | const time::nanoseconds FaceInfo::RTT_NO_MEASUREMENT{-1}; |
| 34 | const time::nanoseconds FaceInfo::RTT_TIMEOUT{-2}; |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 35 | |
Davide Pesavento | a6f637a | 2019-08-28 23:23:20 -0400 | [diff] [blame] | 36 | time::nanoseconds |
| 37 | FaceInfo::scheduleTimeout(const Name& interestName, scheduler::EventCallback cb) |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 38 | { |
Davide Pesavento | a6f637a | 2019-08-28 23:23:20 -0400 | [diff] [blame] | 39 | 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 Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | void |
Davide Pesavento | a6f637a | 2019-08-28 23:23:20 -0400 | [diff] [blame] | 46 | FaceInfo::cancelTimeout(const Name& prefix) |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 47 | { |
Davide Pesavento | a6f637a | 2019-08-28 23:23:20 -0400 | [diff] [blame] | 48 | if (m_lastInterestName.isPrefixOf(prefix)) { |
| 49 | m_timeoutEvent.cancel(); |
| 50 | } |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | //////////////////////////////////////////////////////////////////////////////// |
| 54 | //////////////////////////////////////////////////////////////////////////////// |
| 55 | |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 56 | FaceInfo* |
Davide Pesavento | a6f637a | 2019-08-28 23:23:20 -0400 | [diff] [blame] | 57 | NamespaceInfo::getFaceInfo(FaceId faceId) |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 58 | { |
Davide Pesavento | a6f637a | 2019-08-28 23:23:20 -0400 | [diff] [blame] | 59 | auto it = m_fiMap.find(faceId); |
| 60 | return it != m_fiMap.end() ? &it->second : nullptr; |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | FaceInfo& |
Davide Pesavento | a6f637a | 2019-08-28 23:23:20 -0400 | [diff] [blame] | 64 | NamespaceInfo::getOrCreateFaceInfo(FaceId faceId) |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 65 | { |
Davide Pesavento | a6f637a | 2019-08-28 23:23:20 -0400 | [diff] [blame] | 66 | 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 Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 72 | } |
Davide Pesavento | a6f637a | 2019-08-28 23:23:20 -0400 | [diff] [blame] | 73 | return faceInfo; |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | void |
Ashlesh Gawande | 92e4ea5 | 2017-07-19 11:38:12 -0500 | [diff] [blame] | 77 | NamespaceInfo::extendFaceInfoLifetime(FaceInfo& info, FaceId faceId) |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 78 | { |
Davide Pesavento | a6f637a | 2019-08-28 23:23:20 -0400 | [diff] [blame] | 79 | info.m_measurementExpiration = getScheduler().schedule(AsfMeasurements::MEASUREMENTS_LIFETIME, |
| 80 | [=] { m_fiMap.erase(faceId); }); |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | //////////////////////////////////////////////////////////////////////////////// |
| 84 | //////////////////////////////////////////////////////////////////////////////// |
| 85 | |
| 86 | constexpr time::microseconds AsfMeasurements::MEASUREMENTS_LIFETIME; |
| 87 | |
| 88 | AsfMeasurements::AsfMeasurements(MeasurementsAccessor& measurements) |
| 89 | : m_measurements(measurements) |
Davide Pesavento | a6f637a | 2019-08-28 23:23:20 -0400 | [diff] [blame] | 90 | , m_rttEstimatorOpts(make_shared<ndn::util::RttEstimator::Options>()) |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 91 | { |
| 92 | } |
| 93 | |
| 94 | FaceInfo* |
Saurab Dulal | 432be57 | 2021-01-26 12:09:29 -0600 | [diff] [blame^] | 95 | AsfMeasurements::getFaceInfo(const fib::Entry& fibEntry, const Name& interestName, FaceId faceId) |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 96 | { |
Saurab Dulal | 432be57 | 2021-01-26 12:09:29 -0600 | [diff] [blame^] | 97 | return getOrCreateNamespaceInfo(fibEntry, interestName).getFaceInfo(faceId); |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | FaceInfo& |
Saurab Dulal | 432be57 | 2021-01-26 12:09:29 -0600 | [diff] [blame^] | 101 | AsfMeasurements::getOrCreateFaceInfo(const fib::Entry& fibEntry, const Name& interestName, FaceId faceId) |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 102 | { |
Saurab Dulal | 432be57 | 2021-01-26 12:09:29 -0600 | [diff] [blame^] | 103 | return getOrCreateNamespaceInfo(fibEntry, interestName).getOrCreateFaceInfo(faceId); |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 104 | } |
| 105 | |
Junxiao Shi | fc02186 | 2016-08-25 21:51:18 +0000 | [diff] [blame] | 106 | NamespaceInfo* |
| 107 | AsfMeasurements::getNamespaceInfo(const Name& prefix) |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 108 | { |
Saurab Dulal | 432be57 | 2021-01-26 12:09:29 -0600 | [diff] [blame^] | 109 | auto* me = m_measurements.findLongestPrefixMatch(prefix); |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 110 | if (me == nullptr) { |
| 111 | return nullptr; |
| 112 | } |
| 113 | |
| 114 | // Set or update entry lifetime |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 115 | extendLifetime(*me); |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 116 | |
Davide Pesavento | a6f637a | 2019-08-28 23:23:20 -0400 | [diff] [blame] | 117 | NamespaceInfo* info = me->insertStrategyInfo<NamespaceInfo>(m_rttEstimatorOpts).first; |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 118 | BOOST_ASSERT(info != nullptr); |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 119 | return info; |
| 120 | } |
| 121 | |
| 122 | NamespaceInfo& |
Saurab Dulal | 432be57 | 2021-01-26 12:09:29 -0600 | [diff] [blame^] | 123 | AsfMeasurements::getOrCreateNamespaceInfo(const fib::Entry& fibEntry, const Name& prefix) |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 124 | { |
Saurab Dulal | 432be57 | 2021-01-26 12:09:29 -0600 | [diff] [blame^] | 125 | auto* me = m_measurements.get(fibEntry); |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 126 | |
| 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 Dulal | 432be57 | 2021-01-26 12:09:29 -0600 | [diff] [blame^] | 130 | me == nullptr && prefixLen <= prefix.size(); |
| 131 | ++prefixLen) { |
| 132 | me = m_measurements.get(prefix.getPrefix(prefixLen)); |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 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 Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 139 | extendLifetime(*me); |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 140 | |
Davide Pesavento | a6f637a | 2019-08-28 23:23:20 -0400 | [diff] [blame] | 141 | NamespaceInfo* info = me->insertStrategyInfo<NamespaceInfo>(m_rttEstimatorOpts).first; |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 142 | BOOST_ASSERT(info != nullptr); |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 143 | return *info; |
| 144 | } |
| 145 | |
| 146 | void |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 147 | AsfMeasurements::extendLifetime(measurements::Entry& me) |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 148 | { |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 149 | m_measurements.extendLifetime(me, MEASUREMENTS_LIFETIME); |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | } // namespace asf |
| 153 | } // namespace fw |
| 154 | } // namespace nfd |