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 | b8bd5ee | 2019-02-03 22:53:40 -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-probing-module.hpp" |
Ashlesh Gawande | 2a73f35 | 2016-12-01 15:37:03 +0000 | [diff] [blame] | 27 | #include "algorithm.hpp" |
Davide Pesavento | 2cae8ca | 2019-04-18 20:48:05 -0400 | [diff] [blame] | 28 | #include "common/global.hpp" |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 29 | |
Davide Pesavento | b8bd5ee | 2019-02-03 22:53:40 -0500 | [diff] [blame] | 30 | #include <ndn-cxx/util/random.hpp> |
| 31 | |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 32 | namespace nfd { |
| 33 | namespace fw { |
| 34 | namespace asf { |
| 35 | |
Ashlesh Gawande | 92e4ea5 | 2017-07-19 11:38:12 -0500 | [diff] [blame] | 36 | constexpr time::milliseconds ProbingModule::DEFAULT_PROBING_INTERVAL; |
| 37 | constexpr time::milliseconds ProbingModule::MIN_PROBING_INTERVAL; |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 38 | |
| 39 | static_assert(ProbingModule::DEFAULT_PROBING_INTERVAL < AsfMeasurements::MEASUREMENTS_LIFETIME, |
| 40 | "ProbingModule::DEFAULT_PROBING_INTERVAL must be less than AsfMeasurements::MEASUREMENTS_LIFETIME"); |
| 41 | |
| 42 | ProbingModule::ProbingModule(AsfMeasurements& measurements) |
| 43 | : m_probingInterval(DEFAULT_PROBING_INTERVAL) |
| 44 | , m_measurements(measurements) |
| 45 | { |
| 46 | } |
| 47 | |
| 48 | void |
Davide Pesavento | a6f637a | 2019-08-28 23:23:20 -0400 | [diff] [blame] | 49 | ProbingModule::scheduleProbe(const fib::Entry& fibEntry, time::milliseconds interval) |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 50 | { |
Junxiao Shi | fc02186 | 2016-08-25 21:51:18 +0000 | [diff] [blame] | 51 | Name prefix = fibEntry.getPrefix(); |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 52 | |
| 53 | // Set the probing flag for the namespace to true after passed interval of time |
Davide Pesavento | 3dade00 | 2019-03-19 11:29:56 -0600 | [diff] [blame] | 54 | getScheduler().schedule(interval, [this, prefix] { |
Junxiao Shi | fc02186 | 2016-08-25 21:51:18 +0000 | [diff] [blame] | 55 | NamespaceInfo* info = m_measurements.getNamespaceInfo(prefix); |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 56 | if (info == nullptr) { |
Davide Pesavento | a6f637a | 2019-08-28 23:23:20 -0400 | [diff] [blame] | 57 | // FIB entry with the passed prefix has been removed or |
| 58 | // it has a name that is not controlled by the AsfStrategy |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 59 | } |
| 60 | else { |
| 61 | info->setIsProbingDue(true); |
| 62 | } |
| 63 | }); |
| 64 | } |
| 65 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 66 | Face* |
Davide Pesavento | 3dade00 | 2019-03-19 11:29:56 -0600 | [diff] [blame] | 67 | ProbingModule::getFaceToProbe(const Face& inFace, const Interest& interest, |
| 68 | const fib::Entry& fibEntry, const Face& faceUsed) |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 69 | { |
Davide Pesavento | a6f637a | 2019-08-28 23:23:20 -0400 | [diff] [blame] | 70 | FaceInfoFacePairSet rankedFaces; |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 71 | |
| 72 | // Put eligible faces into rankedFaces. If a face does not have an RTT measurement, |
| 73 | // immediately pick the face for probing |
Davide Pesavento | 3dade00 | 2019-03-19 11:29:56 -0600 | [diff] [blame] | 74 | for (const auto& hop : fibEntry.getNextHops()) { |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 75 | Face& hopFace = hop.getFace(); |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 76 | |
| 77 | // Don't send probe Interest back to the incoming face or use the same face |
Ashlesh Gawande | 2a73f35 | 2016-12-01 15:37:03 +0000 | [diff] [blame] | 78 | // as the forwarded Interest or use a face that violates scope |
| 79 | if (hopFace.getId() == inFace.getId() || hopFace.getId() == faceUsed.getId() || |
| 80 | wouldViolateScope(inFace, interest, hopFace)) { |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 81 | continue; |
| 82 | } |
| 83 | |
Ashlesh Gawande | 92e4ea5 | 2017-07-19 11:38:12 -0500 | [diff] [blame] | 84 | FaceInfo* info = m_measurements.getFaceInfo(fibEntry, interest, hopFace.getId()); |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 85 | // If no RTT has been recorded, probe this face |
Davide Pesavento | a6f637a | 2019-08-28 23:23:20 -0400 | [diff] [blame] | 86 | if (info == nullptr || info->getLastRtt() == FaceInfo::RTT_NO_MEASUREMENT) { |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 87 | return &hopFace; |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | // Add FaceInfo to container sorted by RTT |
Davide Pesavento | e4b2238 | 2018-06-10 14:37:24 -0400 | [diff] [blame] | 91 | rankedFaces.insert({info, &hopFace}); |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | if (rankedFaces.empty()) { |
| 95 | // No Face to probe |
| 96 | return nullptr; |
| 97 | } |
| 98 | |
Davide Pesavento | a6f637a | 2019-08-28 23:23:20 -0400 | [diff] [blame] | 99 | return chooseFace(rankedFaces); |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | bool |
Junxiao Shi | fc02186 | 2016-08-25 21:51:18 +0000 | [diff] [blame] | 103 | ProbingModule::isProbingNeeded(const fib::Entry& fibEntry, const Interest& interest) |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 104 | { |
| 105 | // Return the probing status flag for a namespace |
Junxiao Shi | 8d84314 | 2016-07-11 22:42:42 +0000 | [diff] [blame] | 106 | NamespaceInfo& info = m_measurements.getOrCreateNamespaceInfo(fibEntry, interest); |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 107 | |
| 108 | // If a first probe has not been scheduled for a namespace |
| 109 | if (!info.isFirstProbeScheduled()) { |
| 110 | // Schedule first probe between 0 and 5 seconds |
Davide Pesavento | a6f637a | 2019-08-28 23:23:20 -0400 | [diff] [blame] | 111 | static std::uniform_int_distribution<> randDist(0, 5000); |
| 112 | auto interval = randDist(ndn::random::getRandomNumberEngine()); |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 113 | scheduleProbe(fibEntry, time::milliseconds(interval)); |
Davide Pesavento | a6f637a | 2019-08-28 23:23:20 -0400 | [diff] [blame] | 114 | info.setIsFirstProbeScheduled(true); |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | return info.isProbingDue(); |
| 118 | } |
| 119 | |
| 120 | void |
Junxiao Shi | fc02186 | 2016-08-25 21:51:18 +0000 | [diff] [blame] | 121 | ProbingModule::afterForwardingProbe(const fib::Entry& fibEntry, const Interest& interest) |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 122 | { |
| 123 | // After probing is done, need to set probing flag to false and |
| 124 | // schedule another future probe |
Junxiao Shi | 8d84314 | 2016-07-11 22:42:42 +0000 | [diff] [blame] | 125 | NamespaceInfo& info = m_measurements.getOrCreateNamespaceInfo(fibEntry, interest); |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 126 | info.setIsProbingDue(false); |
| 127 | |
| 128 | scheduleProbe(fibEntry, m_probingInterval); |
| 129 | } |
| 130 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 131 | Face* |
Davide Pesavento | a6f637a | 2019-08-28 23:23:20 -0400 | [diff] [blame] | 132 | ProbingModule::chooseFace(const FaceInfoFacePairSet& rankedFaces) |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 133 | { |
Davide Pesavento | a6f637a | 2019-08-28 23:23:20 -0400 | [diff] [blame] | 134 | static std::uniform_real_distribution<> randDist; |
| 135 | double randomNumber = randDist(ndn::random::getRandomNumberEngine()); |
| 136 | uint64_t rankSum = (rankedFaces.size() + 1) * rankedFaces.size() / 2; |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 137 | |
| 138 | uint64_t rank = 1; |
| 139 | double offset = 0.0; |
| 140 | |
Davide Pesavento | e4b2238 | 2018-06-10 14:37:24 -0400 | [diff] [blame] | 141 | for (const auto& pair : rankedFaces) { |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 142 | double probability = getProbingProbability(rank++, rankSum, rankedFaces.size()); |
| 143 | |
| 144 | // Is the random number within the bounds of this face's probability + the previous faces' |
| 145 | // probability? |
| 146 | // |
| 147 | // e.g. (FaceId: 1, p=0.5), (FaceId: 2, p=0.33), (FaceId: 3, p=0.17) |
| 148 | // randomNumber = 0.92 |
| 149 | // |
| 150 | // The face with FaceId: 3 should be picked |
| 151 | // (0.68 < 0.5 + 0.33 + 0.17) == true |
| 152 | // |
| 153 | if (randomNumber <= offset + probability) { |
| 154 | // Found face to probe |
| 155 | return pair.second; |
| 156 | } |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 157 | offset += probability; |
| 158 | } |
| 159 | |
| 160 | // Given a set of Faces, this method should always select a Face to probe |
Davide Pesavento | a6f637a | 2019-08-28 23:23:20 -0400 | [diff] [blame] | 161 | NDN_CXX_UNREACHABLE; |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | double |
| 165 | ProbingModule::getProbingProbability(uint64_t rank, uint64_t rankSum, uint64_t nFaces) |
| 166 | { |
| 167 | // p = n + 1 - j ; n: # faces |
| 168 | // --------- |
| 169 | // sum(ranks) |
| 170 | return static_cast<double>(nFaces + 1 - rank) / rankSum; |
| 171 | } |
| 172 | |
Ashlesh Gawande | 92e4ea5 | 2017-07-19 11:38:12 -0500 | [diff] [blame] | 173 | void |
| 174 | ProbingModule::setProbingInterval(size_t probingInterval) |
| 175 | { |
| 176 | if (time::milliseconds(probingInterval) >= MIN_PROBING_INTERVAL) { |
| 177 | m_probingInterval = time::milliseconds(probingInterval); |
| 178 | } |
| 179 | else { |
Davide Pesavento | a6f637a | 2019-08-28 23:23:20 -0400 | [diff] [blame] | 180 | NDN_THROW(std::invalid_argument("Probing interval must be >= " + |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 181 | to_string(MIN_PROBING_INTERVAL.count()) + " milliseconds")); |
Ashlesh Gawande | 92e4ea5 | 2017-07-19 11:38:12 -0500 | [diff] [blame] | 182 | } |
| 183 | } |
| 184 | |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 185 | } // namespace asf |
| 186 | } // namespace fw |
| 187 | } // namespace nfd |