blob: dd2b537725bf8e98520a8a153edb84ef5ee34a77 [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 Pesaventob8bd5ee2019-02-03 22:53:40 -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-probing-module.hpp"
Ashlesh Gawande2a73f352016-12-01 15:37:03 +000027#include "algorithm.hpp"
Vince Lehman8a4c29e2016-07-11 08:49:35 +000028
Davide Pesaventob8bd5ee2019-02-03 22:53:40 -050029#include <ndn-cxx/util/random.hpp>
30
Vince Lehman8a4c29e2016-07-11 08:49:35 +000031namespace nfd {
32namespace fw {
33namespace asf {
34
Ashlesh Gawande92e4ea52017-07-19 11:38:12 -050035constexpr time::milliseconds ProbingModule::DEFAULT_PROBING_INTERVAL;
36constexpr time::milliseconds ProbingModule::MIN_PROBING_INTERVAL;
Vince Lehman8a4c29e2016-07-11 08:49:35 +000037
38static_assert(ProbingModule::DEFAULT_PROBING_INTERVAL < AsfMeasurements::MEASUREMENTS_LIFETIME,
39 "ProbingModule::DEFAULT_PROBING_INTERVAL must be less than AsfMeasurements::MEASUREMENTS_LIFETIME");
40
41ProbingModule::ProbingModule(AsfMeasurements& measurements)
42 : m_probingInterval(DEFAULT_PROBING_INTERVAL)
43 , m_measurements(measurements)
44{
45}
46
47void
Junxiao Shi8d843142016-07-11 22:42:42 +000048ProbingModule::scheduleProbe(const fib::Entry& fibEntry, const time::milliseconds& interval)
Vince Lehman8a4c29e2016-07-11 08:49:35 +000049{
Junxiao Shifc021862016-08-25 21:51:18 +000050 Name prefix = fibEntry.getPrefix();
Vince Lehman8a4c29e2016-07-11 08:49:35 +000051
52 // Set the probing flag for the namespace to true after passed interval of time
53 scheduler::schedule(interval, [this, prefix] {
Junxiao Shifc021862016-08-25 21:51:18 +000054 NamespaceInfo* info = m_measurements.getNamespaceInfo(prefix);
Vince Lehman8a4c29e2016-07-11 08:49:35 +000055
56 if (info == nullptr) {
57 // fib::Entry with the passed prefix has been removed or the fib::Entry has
58 // a name that is not controlled by the AsfStrategy
59 return;
60 }
61 else {
62 info->setIsProbingDue(true);
63 }
64 });
65}
66
Junxiao Shia6de4292016-07-12 02:08:10 +000067Face*
Vince Lehman8a4c29e2016-07-11 08:49:35 +000068ProbingModule::getFaceToProbe(const Face& inFace,
69 const Interest& interest,
Junxiao Shi8d843142016-07-11 22:42:42 +000070 const fib::Entry& fibEntry,
Vince Lehman8a4c29e2016-07-11 08:49:35 +000071 const Face& faceUsed)
72{
73 FaceInfoFacePairSet rankedFaces(
Davide Pesaventoe4b22382018-06-10 14:37:24 -040074 [] (const auto& pairLhs, const auto& pairRhs) -> bool {
Vince Lehman8a4c29e2016-07-11 08:49:35 +000075 // Sort by RTT
76 // If a face has timed-out, rank it behind non-timed-out faces
77 FaceInfo& lhs = *pairLhs.first;
78 FaceInfo& rhs = *pairRhs.first;
79
80 return (!lhs.isTimeout() && rhs.isTimeout()) ||
81 (lhs.isTimeout() == rhs.isTimeout() && lhs.getSrtt() < rhs.getSrtt());
82 });
83
84 // Put eligible faces into rankedFaces. If a face does not have an RTT measurement,
85 // immediately pick the face for probing
Junxiao Shi8d843142016-07-11 22:42:42 +000086 for (const fib::NextHop& hop : fibEntry.getNextHops()) {
Junxiao Shia6de4292016-07-12 02:08:10 +000087 Face& hopFace = hop.getFace();
Vince Lehman8a4c29e2016-07-11 08:49:35 +000088
89 // Don't send probe Interest back to the incoming face or use the same face
Ashlesh Gawande2a73f352016-12-01 15:37:03 +000090 // as the forwarded Interest or use a face that violates scope
91 if (hopFace.getId() == inFace.getId() || hopFace.getId() == faceUsed.getId() ||
92 wouldViolateScope(inFace, interest, hopFace)) {
Vince Lehman8a4c29e2016-07-11 08:49:35 +000093 continue;
94 }
95
Ashlesh Gawande92e4ea52017-07-19 11:38:12 -050096 FaceInfo* info = m_measurements.getFaceInfo(fibEntry, interest, hopFace.getId());
Vince Lehman8a4c29e2016-07-11 08:49:35 +000097 // If no RTT has been recorded, probe this face
98 if (info == nullptr || !info->hasSrttMeasurement()) {
Junxiao Shia6de4292016-07-12 02:08:10 +000099 return &hopFace;
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000100 }
101
102 // Add FaceInfo to container sorted by RTT
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400103 rankedFaces.insert({info, &hopFace});
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000104 }
105
106 if (rankedFaces.empty()) {
107 // No Face to probe
108 return nullptr;
109 }
110
111 return getFaceBasedOnProbability(rankedFaces);
112}
113
114bool
Junxiao Shifc021862016-08-25 21:51:18 +0000115ProbingModule::isProbingNeeded(const fib::Entry& fibEntry, const Interest& interest)
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000116{
117 // Return the probing status flag for a namespace
Junxiao Shi8d843142016-07-11 22:42:42 +0000118 NamespaceInfo& info = m_measurements.getOrCreateNamespaceInfo(fibEntry, interest);
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000119
120 // If a first probe has not been scheduled for a namespace
121 if (!info.isFirstProbeScheduled()) {
122 // Schedule first probe between 0 and 5 seconds
123 uint64_t interval = getRandomNumber(0, 5000);
124 scheduleProbe(fibEntry, time::milliseconds(interval));
125
126 info.setHasFirstProbeBeenScheduled(true);
127 }
128
129 return info.isProbingDue();
130}
131
132void
Junxiao Shifc021862016-08-25 21:51:18 +0000133ProbingModule::afterForwardingProbe(const fib::Entry& fibEntry, const Interest& interest)
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000134{
135 // After probing is done, need to set probing flag to false and
136 // schedule another future probe
Junxiao Shi8d843142016-07-11 22:42:42 +0000137 NamespaceInfo& info = m_measurements.getOrCreateNamespaceInfo(fibEntry, interest);
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000138 info.setIsProbingDue(false);
139
140 scheduleProbe(fibEntry, m_probingInterval);
141}
142
Junxiao Shia6de4292016-07-12 02:08:10 +0000143Face*
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000144ProbingModule::getFaceBasedOnProbability(const FaceInfoFacePairSet& rankedFaces)
145{
146 double randomNumber = getRandomNumber(0, 1);
147 uint64_t rankSum = ((rankedFaces.size() + 1) * rankedFaces.size()) / 2;
148
149 uint64_t rank = 1;
150 double offset = 0.0;
151
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400152 for (const auto& pair : rankedFaces) {
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000153 double probability = getProbingProbability(rank++, rankSum, rankedFaces.size());
154
155 // Is the random number within the bounds of this face's probability + the previous faces'
156 // probability?
157 //
158 // e.g. (FaceId: 1, p=0.5), (FaceId: 2, p=0.33), (FaceId: 3, p=0.17)
159 // randomNumber = 0.92
160 //
161 // The face with FaceId: 3 should be picked
162 // (0.68 < 0.5 + 0.33 + 0.17) == true
163 //
164 if (randomNumber <= offset + probability) {
165 // Found face to probe
166 return pair.second;
167 }
168
169 offset += probability;
170 }
171
172 // Given a set of Faces, this method should always select a Face to probe
173 BOOST_ASSERT(false);
174 return nullptr;
175}
176
177double
178ProbingModule::getProbingProbability(uint64_t rank, uint64_t rankSum, uint64_t nFaces)
179{
180 // p = n + 1 - j ; n: # faces
181 // ---------
182 // sum(ranks)
183 return static_cast<double>(nFaces + 1 - rank) / rankSum;
184}
185
186double
187ProbingModule::getRandomNumber(double start, double end)
188{
Davide Pesavento5f47aa62016-10-07 22:09:09 +0200189 std::uniform_real_distribution<double> dist(start, end);
Davide Pesaventob8bd5ee2019-02-03 22:53:40 -0500190 return dist(ndn::random::getRandomNumberEngine());
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000191}
192
Ashlesh Gawande92e4ea52017-07-19 11:38:12 -0500193void
194ProbingModule::setProbingInterval(size_t probingInterval)
195{
196 if (time::milliseconds(probingInterval) >= MIN_PROBING_INTERVAL) {
197 m_probingInterval = time::milliseconds(probingInterval);
198 }
199 else {
Davide Pesavento19779d82019-02-14 13:40:04 -0500200 NDN_THROW(std::invalid_argument("Probing interval should be >= " +
201 to_string(MIN_PROBING_INTERVAL.count()) + " milliseconds"));
Ashlesh Gawande92e4ea52017-07-19 11:38:12 -0500202 }
203}
204
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000205} // namespace asf
206} // namespace fw
207} // namespace nfd