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 | /* |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 3 | * Copyright (c) 2014-2019, 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* |
Ashlesh Gawande | 92e4ea5 | 2017-07-19 11:38:12 -0500 | [diff] [blame] | 95 | AsfMeasurements::getFaceInfo(const fib::Entry& fibEntry, const Interest& interest, FaceId faceId) |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 96 | { |
Davide Pesavento | a6f637a | 2019-08-28 23:23:20 -0400 | [diff] [blame] | 97 | return getOrCreateNamespaceInfo(fibEntry, interest).getFaceInfo(faceId); |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | FaceInfo& |
Ashlesh Gawande | 92e4ea5 | 2017-07-19 11:38:12 -0500 | [diff] [blame] | 101 | AsfMeasurements::getOrCreateFaceInfo(const fib::Entry& fibEntry, const Interest& interest, |
| 102 | FaceId faceId) |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 103 | { |
Davide Pesavento | a6f637a | 2019-08-28 23:23:20 -0400 | [diff] [blame] | 104 | return getOrCreateNamespaceInfo(fibEntry, interest).getOrCreateFaceInfo(faceId); |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 105 | } |
| 106 | |
Junxiao Shi | fc02186 | 2016-08-25 21:51:18 +0000 | [diff] [blame] | 107 | NamespaceInfo* |
| 108 | AsfMeasurements::getNamespaceInfo(const Name& prefix) |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 109 | { |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 110 | measurements::Entry* me = m_measurements.findLongestPrefixMatch(prefix); |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 111 | if (me == nullptr) { |
| 112 | return nullptr; |
| 113 | } |
| 114 | |
| 115 | // Set or update entry lifetime |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 116 | extendLifetime(*me); |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 117 | |
Davide Pesavento | a6f637a | 2019-08-28 23:23:20 -0400 | [diff] [blame] | 118 | NamespaceInfo* info = me->insertStrategyInfo<NamespaceInfo>(m_rttEstimatorOpts).first; |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 119 | BOOST_ASSERT(info != nullptr); |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 120 | return info; |
| 121 | } |
| 122 | |
| 123 | NamespaceInfo& |
Junxiao Shi | fc02186 | 2016-08-25 21:51:18 +0000 | [diff] [blame] | 124 | AsfMeasurements::getOrCreateNamespaceInfo(const fib::Entry& fibEntry, const Interest& interest) |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 125 | { |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 126 | measurements::Entry* me = m_measurements.get(fibEntry); |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 127 | |
| 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 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 |