blob: 46acaf4a93de86901d712934f0ef96fd1afb926f [file] [log] [blame]
Vince Lehman8a4c29e2016-07-11 08:49:35 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Ashlesh Gawande90015992017-07-11 17:25:48 -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-strategy.hpp"
Ashlesh Gawande2a73f352016-12-01 15:37:03 +000027#include "algorithm.hpp"
Vince Lehman8a4c29e2016-07-11 08:49:35 +000028#include "core/logger.hpp"
Davide Pesavento3dade002019-03-19 11:29:56 -060029#include "daemon/global.hpp"
Vince Lehman8a4c29e2016-07-11 08:49:35 +000030
31namespace nfd {
32namespace fw {
33namespace asf {
34
Davide Pesaventoa3148082018-04-12 18:21:54 -040035NFD_LOG_INIT(AsfStrategy);
Junxiao Shi037f4ab2016-12-13 04:27:06 +000036NFD_REGISTER_STRATEGY(AsfStrategy);
Vince Lehman8a4c29e2016-07-11 08:49:35 +000037
Vince Lehman8a4c29e2016-07-11 08:49:35 +000038const time::milliseconds AsfStrategy::RETX_SUPPRESSION_INITIAL(10);
39const time::milliseconds AsfStrategy::RETX_SUPPRESSION_MAX(250);
40
Vince Lehman8a4c29e2016-07-11 08:49:35 +000041AsfStrategy::AsfStrategy(Forwarder& forwarder, const Name& name)
Junxiao Shi18739c42016-12-22 08:03:00 +000042 : Strategy(forwarder)
Vince Lehman8a4c29e2016-07-11 08:49:35 +000043 , m_measurements(getMeasurements())
44 , m_probing(m_measurements)
Ashlesh Gawande92e4ea52017-07-19 11:38:12 -050045 , m_maxSilentTimeouts(0)
Vince Lehman8a4c29e2016-07-11 08:49:35 +000046 , m_retxSuppression(RETX_SUPPRESSION_INITIAL,
47 RetxSuppressionExponential::DEFAULT_MULTIPLIER,
48 RETX_SUPPRESSION_MAX)
49{
Junxiao Shi18739c42016-12-22 08:03:00 +000050 ParsedInstanceName parsed = parseInstanceName(name);
51 if (!parsed.parameters.empty()) {
Ashlesh Gawande92e4ea52017-07-19 11:38:12 -050052 processParams(parsed.parameters);
Junxiao Shi18739c42016-12-22 08:03:00 +000053 }
Ashlesh Gawande92e4ea52017-07-19 11:38:12 -050054
Junxiao Shi91f6ee02016-12-29 21:44:44 +000055 if (parsed.version && *parsed.version != getStrategyName()[-1].toVersion()) {
Davide Pesavento19779d82019-02-14 13:40:04 -050056 NDN_THROW(std::invalid_argument(
Alexander Afanasyev0c63c632017-12-05 11:17:09 -050057 "AsfStrategy does not support version " + to_string(*parsed.version)));
Junxiao Shi91f6ee02016-12-29 21:44:44 +000058 }
Junxiao Shi18739c42016-12-22 08:03:00 +000059 this->setInstanceName(makeInstanceName(name, getStrategyName()));
Ashlesh Gawande92e4ea52017-07-19 11:38:12 -050060
61 NFD_LOG_DEBUG("Probing interval=" << m_probing.getProbingInterval()
62 << ", Num silent timeouts=" << m_maxSilentTimeouts);
Vince Lehman8a4c29e2016-07-11 08:49:35 +000063}
64
Junxiao Shi037f4ab2016-12-13 04:27:06 +000065const Name&
66AsfStrategy::getStrategyName()
67{
Ashlesh Gawande92e4ea52017-07-19 11:38:12 -050068 static Name strategyName("/localhost/nfd/strategy/asf/%FD%03");
Junxiao Shi037f4ab2016-12-13 04:27:06 +000069 return strategyName;
70}
71
Vince Lehman8a4c29e2016-07-11 08:49:35 +000072void
Ashlesh Gawande92e4ea52017-07-19 11:38:12 -050073AsfStrategy::processParams(const PartialName& parsed)
74{
75 for (const auto& component : parsed) {
76 std::string parsedStr(reinterpret_cast<const char*>(component.value()), component.value_size());
77 auto n = parsedStr.find("~");
78 if (n == std::string::npos) {
Davide Pesavento19779d82019-02-14 13:40:04 -050079 NDN_THROW(std::invalid_argument("Format is <parameter>~<value>"));
Ashlesh Gawande92e4ea52017-07-19 11:38:12 -050080 }
81
82 auto f = parsedStr.substr(0, n);
83 auto s = parsedStr.substr(n + 1);
84 if (f == "probing-interval") {
85 m_probing.setProbingInterval(getParamValue(f, s));
86 }
87 else if (f == "n-silent-timeouts") {
88 m_maxSilentTimeouts = getParamValue(f, s);
89 }
90 else {
Davide Pesavento19779d82019-02-14 13:40:04 -050091 NDN_THROW(std::invalid_argument("Parameter should be probing-interval or n-silent-timeouts"));
Ashlesh Gawande92e4ea52017-07-19 11:38:12 -050092 }
93 }
94}
95
96uint64_t
97AsfStrategy::getParamValue(const std::string& param, const std::string& value)
98{
99 try {
100 if (!value.empty() && value[0] == '-')
Davide Pesavento19779d82019-02-14 13:40:04 -0500101 NDN_THROW(boost::bad_lexical_cast());
Ashlesh Gawande92e4ea52017-07-19 11:38:12 -0500102
103 return boost::lexical_cast<uint64_t>(value);
104 }
105 catch (const boost::bad_lexical_cast&) {
Davide Pesavento19779d82019-02-14 13:40:04 -0500106 NDN_THROW(std::invalid_argument("Value of " + param + " must be a non-negative integer"));
Ashlesh Gawande92e4ea52017-07-19 11:38:12 -0500107 }
108}
109
110void
ashiqopuc7079482019-02-20 05:34:37 +0000111AsfStrategy::afterReceiveInterest(const FaceEndpoint& ingress, const Interest& interest,
Junxiao Shi15e98b02016-08-12 11:21:44 +0000112 const shared_ptr<pit::Entry>& pitEntry)
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000113{
114 // Should the Interest be suppressed?
Ashlesh Gawande90015992017-07-11 17:25:48 -0500115 RetxSuppressionResult suppressResult = m_retxSuppression.decidePerPitEntry(*pitEntry);
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000116
117 switch (suppressResult) {
Ashlesh Gawande90015992017-07-11 17:25:48 -0500118 case RetxSuppressionResult::NEW:
119 case RetxSuppressionResult::FORWARD:
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000120 break;
Ashlesh Gawande90015992017-07-11 17:25:48 -0500121 case RetxSuppressionResult::SUPPRESS:
ashiqopuc7079482019-02-20 05:34:37 +0000122 NFD_LOG_DEBUG(interest << " from=" << ingress << " suppressed");
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000123 return;
124 }
125
Junxiao Shi8d843142016-07-11 22:42:42 +0000126 const fib::Entry& fibEntry = this->lookupFib(*pitEntry);
127 const fib::NextHopList& nexthops = fibEntry.getNextHops();
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000128
129 if (nexthops.size() == 0) {
ashiqopuc7079482019-02-20 05:34:37 +0000130 sendNoRouteNack(ingress, interest, pitEntry);
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000131 this->rejectPendingInterest(pitEntry);
132 return;
133 }
134
ashiqopuc7079482019-02-20 05:34:37 +0000135 Face* faceToUse = getBestFaceForForwarding(fibEntry, interest, ingress.face);
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000136
137 if (faceToUse == nullptr) {
ashiqopuc7079482019-02-20 05:34:37 +0000138 sendNoRouteNack(ingress, interest, pitEntry);
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000139 this->rejectPendingInterest(pitEntry);
140 return;
141 }
142
Ashlesh Gawandecad76b62017-04-04 15:28:30 -0500143 NFD_LOG_TRACE("Forwarding interest to face: " << faceToUse->getId());
144
Junxiao Shia6de4292016-07-12 02:08:10 +0000145 forwardInterest(interest, fibEntry, pitEntry, *faceToUse);
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000146
147 // If necessary, send probe
148 if (m_probing.isProbingNeeded(fibEntry, interest)) {
ashiqopuc7079482019-02-20 05:34:37 +0000149 Face* faceToProbe = m_probing.getFaceToProbe(ingress.face, interest, fibEntry, *faceToUse);
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000150
151 if (faceToProbe != nullptr) {
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000152 bool wantNewNonce = true;
Junxiao Shia6de4292016-07-12 02:08:10 +0000153 forwardInterest(interest, fibEntry, pitEntry, *faceToProbe, wantNewNonce);
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000154 m_probing.afterForwardingProbe(fibEntry, interest);
155 }
156 }
157}
158
159void
Junxiao Shi15e98b02016-08-12 11:21:44 +0000160AsfStrategy::beforeSatisfyInterest(const shared_ptr<pit::Entry>& pitEntry,
ashiqopuc7079482019-02-20 05:34:37 +0000161 const FaceEndpoint& ingress, const Data& data)
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000162{
Junxiao Shifc021862016-08-25 21:51:18 +0000163 NamespaceInfo* namespaceInfo = m_measurements.getNamespaceInfo(pitEntry->getName());
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000164
165 if (namespaceInfo == nullptr) {
166 NFD_LOG_TRACE("Could not find measurements entry for " << pitEntry->getName());
167 return;
168 }
169
170 // Record the RTT between the Interest out to Data in
ashiqopuc7079482019-02-20 05:34:37 +0000171 FaceInfo* faceInfo = namespaceInfo->get(ingress.face.getId());
Ashlesh Gawandecad76b62017-04-04 15:28:30 -0500172 if (faceInfo == nullptr) {
173 return;
174 }
ashiqopuc7079482019-02-20 05:34:37 +0000175 faceInfo->recordRtt(pitEntry, ingress.face);
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000176
177 // Extend lifetime for measurements associated with Face
ashiqopuc7079482019-02-20 05:34:37 +0000178 namespaceInfo->extendFaceInfoLifetime(*faceInfo, ingress.face.getId());
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000179
Ashlesh Gawandecad76b62017-04-04 15:28:30 -0500180 if (faceInfo->isTimeoutScheduled()) {
181 faceInfo->cancelTimeoutEvent(data.getName());
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000182 }
183}
184
185void
ashiqopuc7079482019-02-20 05:34:37 +0000186AsfStrategy::afterReceiveNack(const FaceEndpoint& ingress, const lp::Nack& nack,
Junxiao Shi15e98b02016-08-12 11:21:44 +0000187 const shared_ptr<pit::Entry>& pitEntry)
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000188{
ashiqopuc7079482019-02-20 05:34:37 +0000189 NFD_LOG_DEBUG("Nack for " << nack.getInterest() << " from=" << ingress << ": reason=" << nack.getReason());
190 onTimeout(pitEntry->getName(), ingress.face.getId());
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000191}
192
193////////////////////////////////////////////////////////////////////////////////
194////////////////////////////////////////////////////////////////////////////////
195
196void
197AsfStrategy::forwardInterest(const Interest& interest,
198 const fib::Entry& fibEntry,
Junxiao Shi15e98b02016-08-12 11:21:44 +0000199 const shared_ptr<pit::Entry>& pitEntry,
Junxiao Shia6de4292016-07-12 02:08:10 +0000200 Face& outFace,
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000201 bool wantNewNonce)
202{
ashiqopuc7079482019-02-20 05:34:37 +0000203 auto egress = FaceEndpoint(outFace, 0);
204
Ashlesh Gawande2a73f352016-12-01 15:37:03 +0000205 if (wantNewNonce) {
206 //Send probe: interest with new Nonce
207 Interest probeInterest(interest);
208 probeInterest.refreshNonce();
209 NFD_LOG_TRACE("Sending probe for " << probeInterest << probeInterest.getNonce()
ashiqopuc7079482019-02-20 05:34:37 +0000210 << " to: " << egress);
211 this->sendInterest(pitEntry, egress, probeInterest);
Ashlesh Gawande2a73f352016-12-01 15:37:03 +0000212 }
213 else {
ashiqopuc7079482019-02-20 05:34:37 +0000214 this->sendInterest(pitEntry, egress, interest);
Ashlesh Gawande2a73f352016-12-01 15:37:03 +0000215 }
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000216
ashiqopuc7079482019-02-20 05:34:37 +0000217 FaceInfo& faceInfo = m_measurements.getOrCreateFaceInfo(fibEntry, interest, egress.face.getId());
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000218
219 // Refresh measurements since Face is being used for forwarding
220 NamespaceInfo& namespaceInfo = m_measurements.getOrCreateNamespaceInfo(fibEntry, interest);
ashiqopuc7079482019-02-20 05:34:37 +0000221 namespaceInfo.extendFaceInfoLifetime(faceInfo, egress.face.getId());
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000222
223 if (!faceInfo.isTimeoutScheduled()) {
224 // Estimate and schedule timeout
225 RttEstimator::Duration timeout = faceInfo.computeRto();
226
ashiqopuc7079482019-02-20 05:34:37 +0000227 NFD_LOG_TRACE("Scheduling timeout for " << fibEntry.getPrefix() << " to: " << egress
228 << " in " << time::duration_cast<time::milliseconds>(timeout) << " ms");
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000229
Davide Pesavento3dade002019-03-19 11:29:56 -0600230 auto id = getScheduler().schedule(timeout, bind(&AsfStrategy::onTimeout, this,
231 interest.getName(), egress.face.getId()));
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000232 faceInfo.setTimeoutEvent(id, interest.getName());
233 }
234}
235
236struct FaceStats
237{
Junxiao Shia6de4292016-07-12 02:08:10 +0000238 Face* face;
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000239 RttStats::Rtt rtt;
240 RttStats::Rtt srtt;
241 uint64_t cost;
242};
243
244double
245getValueForSorting(const FaceStats& stats)
246{
247 // These values allow faces with no measurements to be ranked better than timeouts
248 // srtt < RTT_NO_MEASUREMENT < RTT_TIMEOUT
249 static const RttStats::Rtt SORTING_RTT_TIMEOUT = time::microseconds::max();
250 static const RttStats::Rtt SORTING_RTT_NO_MEASUREMENT = SORTING_RTT_TIMEOUT / 2;
251
252 if (stats.rtt == RttStats::RTT_TIMEOUT) {
253 return SORTING_RTT_TIMEOUT.count();
254 }
255 else if (stats.rtt == RttStats::RTT_NO_MEASUREMENT) {
256 return SORTING_RTT_NO_MEASUREMENT.count();
257 }
258 else {
259 return stats.srtt.count();
260 }
261}
262
Junxiao Shia6de4292016-07-12 02:08:10 +0000263Face*
Junxiao Shi15e98b02016-08-12 11:21:44 +0000264AsfStrategy::getBestFaceForForwarding(const fib::Entry& fibEntry, const Interest& interest,
265 const Face& inFace)
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000266{
267 NFD_LOG_TRACE("Looking for best face for " << fibEntry.getPrefix());
268
269 typedef std::function<bool(const FaceStats&, const FaceStats&)> FaceStatsPredicate;
270 typedef std::set<FaceStats, FaceStatsPredicate> FaceStatsSet;
271
272 FaceStatsSet rankedFaces(
273 [] (const FaceStats& lhs, const FaceStats& rhs) -> bool {
274 // Sort by RTT and then by cost
275 double lhsValue = getValueForSorting(lhs);
276 double rhsValue = getValueForSorting(rhs);
277
278 if (lhsValue < rhsValue) {
279 return true;
280 }
281 else if (lhsValue == rhsValue) {
282 return lhs.cost < rhs.cost;
283 }
284 else {
285 return false;
286 }
287 });
288
289 for (const fib::NextHop& hop : fibEntry.getNextHops()) {
Junxiao Shia6de4292016-07-12 02:08:10 +0000290 Face& hopFace = hop.getFace();
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000291
Teng Liangf995f382017-04-04 22:09:39 +0000292 if ((hopFace.getId() == inFace.getId() && hopFace.getLinkType() != ndn::nfd::LINK_TYPE_AD_HOC) ||
293 wouldViolateScope(inFace, interest, hopFace)) {
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000294 continue;
295 }
296
Ashlesh Gawande92e4ea52017-07-19 11:38:12 -0500297 FaceInfo* info = m_measurements.getFaceInfo(fibEntry, interest, hopFace.getId());
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000298
299 if (info == nullptr) {
Junxiao Shia6de4292016-07-12 02:08:10 +0000300 FaceStats stats = {&hopFace,
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000301 RttStats::RTT_NO_MEASUREMENT,
302 RttStats::RTT_NO_MEASUREMENT,
303 hop.getCost()};
304
305 rankedFaces.insert(stats);
306 }
307 else {
Junxiao Shia6de4292016-07-12 02:08:10 +0000308 FaceStats stats = {&hopFace, info->getRtt(), info->getSrtt(), hop.getCost()};
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000309 rankedFaces.insert(stats);
310 }
311 }
312
313 FaceStatsSet::iterator it = rankedFaces.begin();
314
315 if (it != rankedFaces.end()) {
316 return it->face;
317 }
318 else {
319 return nullptr;
320 }
321}
322
323void
Ashlesh Gawande92e4ea52017-07-19 11:38:12 -0500324AsfStrategy::onTimeout(const Name& interestName, const face::FaceId faceId)
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000325{
Junxiao Shifc021862016-08-25 21:51:18 +0000326 NamespaceInfo* namespaceInfo = m_measurements.getNamespaceInfo(interestName);
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000327
328 if (namespaceInfo == nullptr) {
329 NFD_LOG_TRACE("FibEntry for " << interestName << " has been removed since timeout scheduling");
330 return;
331 }
332
333 FaceInfoTable::iterator it = namespaceInfo->find(faceId);
334
335 if (it == namespaceInfo->end()) {
336 it = namespaceInfo->insert(faceId);
337 }
338
339 FaceInfo& faceInfo = it->second;
Ashlesh Gawande92e4ea52017-07-19 11:38:12 -0500340
341 faceInfo.setNSilentTimeouts(faceInfo.getNSilentTimeouts() + 1);
342
343 if (faceInfo.getNSilentTimeouts() <= m_maxSilentTimeouts) {
344 NFD_LOG_TRACE("FaceId " << faceId << " for " << interestName << " has timed-out "
345 << faceInfo.getNSilentTimeouts() << " time(s), ignoring");
346 // Extend lifetime for measurements associated with Face
347 namespaceInfo->extendFaceInfoLifetime(faceInfo, faceId);
348
349 if (faceInfo.isTimeoutScheduled()) {
350 faceInfo.cancelTimeoutEvent(interestName);
351 }
352 }
353 else {
354 NFD_LOG_TRACE("FaceId " << faceId << " for " << interestName << " has timed-out");
355 faceInfo.recordTimeout(interestName);
356 }
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000357}
358
359void
ashiqopuc7079482019-02-20 05:34:37 +0000360AsfStrategy::sendNoRouteNack(const FaceEndpoint& ingress, const Interest& interest,
Junxiao Shi15e98b02016-08-12 11:21:44 +0000361 const shared_ptr<pit::Entry>& pitEntry)
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000362{
ashiqopuc7079482019-02-20 05:34:37 +0000363 NFD_LOG_DEBUG(interest << " from=" << ingress << " noNextHop");
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000364
365 lp::NackHeader nackHeader;
366 nackHeader.setReason(lp::NackReason::NO_ROUTE);
ashiqopuc7079482019-02-20 05:34:37 +0000367 this->sendNack(pitEntry, ingress, nackHeader);
Vince Lehman8a4c29e2016-07-11 08:49:35 +0000368}
369
370} // namespace asf
371} // namespace fw
372} // namespace nfd