blob: a76642497f79023df7104569fc9d6a1e5654b095 [file] [log] [blame]
Junxiao Shi80ee7cb2014-12-14 10:53:05 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Ashlesh Gawande90015992017-07-11 17:25:48 -05002/*
Davide Pesaventoa3148082018-04-12 18:21:54 -04003 * Copyright (c) 2014-2018, Regents of the University of California,
Junxiao Shi80ee7cb2014-12-14 10:53:05 -07004 * 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 "access-strategy.hpp"
Junxiao Shi00dc9142016-11-21 14:23:12 +000027#include "algorithm.hpp"
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070028#include "core/logger.hpp"
29
30namespace nfd {
31namespace fw {
32
Davide Pesaventoa3148082018-04-12 18:21:54 -040033NFD_LOG_INIT(AccessStrategy);
Junxiao Shifaf3eb02015-02-16 10:50:36 -070034NFD_REGISTER_STRATEGY(AccessStrategy);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070035
36AccessStrategy::AccessStrategy(Forwarder& forwarder, const Name& name)
Junxiao Shi18739c42016-12-22 08:03:00 +000037 : Strategy(forwarder)
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070038 , m_removeFaceInfoConn(this->beforeRemoveFace.connect(
39 bind(&AccessStrategy::removeFaceInfo, this, _1)))
40{
Junxiao Shi18739c42016-12-22 08:03:00 +000041 ParsedInstanceName parsed = parseInstanceName(name);
42 if (!parsed.parameters.empty()) {
43 BOOST_THROW_EXCEPTION(std::invalid_argument("AccessStrategy does not accept parameters"));
44 }
Junxiao Shi91f6ee02016-12-29 21:44:44 +000045 if (parsed.version && *parsed.version != getStrategyName()[-1].toVersion()) {
46 BOOST_THROW_EXCEPTION(std::invalid_argument(
Alexander Afanasyev0c63c632017-12-05 11:17:09 -050047 "AccessStrategy does not support version " + to_string(*parsed.version)));
Junxiao Shi91f6ee02016-12-29 21:44:44 +000048 }
Junxiao Shi18739c42016-12-22 08:03:00 +000049 this->setInstanceName(makeInstanceName(name, getStrategyName()));
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070050}
51
Junxiao Shi037f4ab2016-12-13 04:27:06 +000052const Name&
53AccessStrategy::getStrategyName()
54{
55 static Name strategyName("/localhost/nfd/strategy/access/%FD%01");
56 return strategyName;
57}
58
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070059void
Junxiao Shi15e98b02016-08-12 11:21:44 +000060AccessStrategy::afterReceiveInterest(const Face& inFace, const Interest& interest,
61 const shared_ptr<pit::Entry>& pitEntry)
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070062{
Ashlesh Gawande90015992017-07-11 17:25:48 -050063 RetxSuppressionResult suppressResult = m_retxSuppression.decidePerPitEntry(*pitEntry);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070064 switch (suppressResult) {
Ashlesh Gawande90015992017-07-11 17:25:48 -050065 case RetxSuppressionResult::NEW:
Junxiao Shi8d843142016-07-11 22:42:42 +000066 this->afterReceiveNewInterest(inFace, interest, pitEntry);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070067 break;
Ashlesh Gawande90015992017-07-11 17:25:48 -050068 case RetxSuppressionResult::FORWARD:
Junxiao Shi8d843142016-07-11 22:42:42 +000069 this->afterReceiveRetxInterest(inFace, interest, pitEntry);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070070 break;
Ashlesh Gawande90015992017-07-11 17:25:48 -050071 case RetxSuppressionResult::SUPPRESS:
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070072 NFD_LOG_DEBUG(interest << " interestFrom " << inFace.getId() << " retx-suppress");
73 break;
74 default:
75 BOOST_ASSERT(false);
76 break;
77 }
78}
79
80void
Junxiao Shi15e98b02016-08-12 11:21:44 +000081AccessStrategy::afterReceiveNewInterest(const Face& inFace, const Interest& interest,
82 const shared_ptr<pit::Entry>& pitEntry)
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070083{
Junxiao Shi8d843142016-07-11 22:42:42 +000084 const fib::Entry& fibEntry = this->lookupFib(*pitEntry);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070085 Name miName;
Junxiao Shifc021862016-08-25 21:51:18 +000086 MtInfo* mi = nullptr;
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070087 std::tie(miName, mi) = this->findPrefixMeasurements(*pitEntry);
88
89 // has measurements for Interest Name?
90 if (mi != nullptr) {
91 NFD_LOG_DEBUG(interest << " interestFrom " << inFace.getId() <<
92 " new-interest mi=" << miName);
93
94 // send to last working nexthop
Junxiao Shia7f9a292016-11-22 16:31:38 +000095 bool isSentToLastNexthop = this->sendToLastNexthop(inFace, interest, pitEntry, *mi, fibEntry);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070096
97 if (isSentToLastNexthop) {
98 return;
99 }
100 }
101 else {
102 NFD_LOG_DEBUG(interest << " interestFrom " << inFace.getId() <<
103 " new-interest no-mi");
104 }
105
106 // no measurements, or last working nexthop unavailable
107
Junxiao Shi965d3a42015-06-01 06:55:23 -0700108 // multicast to all nexthops except incoming face
Junxiao Shia7f9a292016-11-22 16:31:38 +0000109 int nMulticastSent = this->multicast(inFace, interest, pitEntry, fibEntry);
110
111 if (nMulticastSent < 1) {
112 this->rejectPendingInterest(pitEntry);
113 }
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700114}
115
116void
Junxiao Shi15e98b02016-08-12 11:21:44 +0000117AccessStrategy::afterReceiveRetxInterest(const Face& inFace, const Interest& interest,
118 const shared_ptr<pit::Entry>& pitEntry)
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700119{
Junxiao Shi8d843142016-07-11 22:42:42 +0000120 const fib::Entry& fibEntry = this->lookupFib(*pitEntry);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700121 NFD_LOG_DEBUG(interest << " interestFrom " << inFace.getId() << " retx-forward");
Junxiao Shia7f9a292016-11-22 16:31:38 +0000122 this->multicast(inFace, interest, pitEntry, fibEntry);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700123}
124
125bool
Junxiao Shia7f9a292016-11-22 16:31:38 +0000126AccessStrategy::sendToLastNexthop(const Face& inFace, const Interest& interest,
127 const shared_ptr<pit::Entry>& pitEntry, MtInfo& mi,
128 const fib::Entry& fibEntry)
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700129{
Junxiao Shicde37ad2015-12-24 01:02:05 -0700130 if (mi.lastNexthop == face::INVALID_FACEID) {
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700131 NFD_LOG_DEBUG(pitEntry->getInterest() << " no-last-nexthop");
132 return false;
133 }
134
135 if (mi.lastNexthop == inFace.getId()) {
136 NFD_LOG_DEBUG(pitEntry->getInterest() << " last-nexthop-is-downstream");
137 return false;
138 }
139
Junxiao Shia7f9a292016-11-22 16:31:38 +0000140 Face* outFace = this->getFace(mi.lastNexthop);
141 if (outFace == nullptr || !fibEntry.hasNextHop(*outFace)) {
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700142 NFD_LOG_DEBUG(pitEntry->getInterest() << " last-nexthop-gone");
143 return false;
144 }
145
Junxiao Shia7f9a292016-11-22 16:31:38 +0000146 if (wouldViolateScope(inFace, interest, *outFace)) {
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700147 NFD_LOG_DEBUG(pitEntry->getInterest() << " last-nexthop-violates-scope");
148 return false;
149 }
150
151 RttEstimator::Duration rto = mi.rtt.computeRto();
152 NFD_LOG_DEBUG(pitEntry->getInterest() << " interestTo " << mi.lastNexthop <<
153 " last-nexthop rto=" << time::duration_cast<time::microseconds>(rto).count());
154
Junxiao Shia7f9a292016-11-22 16:31:38 +0000155 this->sendInterest(pitEntry, *outFace, interest);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700156
157 // schedule RTO timeout
Junxiao Shifc021862016-08-25 21:51:18 +0000158 PitInfo* pi = pitEntry->insertStrategyInfo<PitInfo>().first;
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700159 pi->rtoTimer = scheduler::schedule(rto,
160 bind(&AccessStrategy::afterRtoTimeout, this, weak_ptr<pit::Entry>(pitEntry),
Junxiao Shi8d843142016-07-11 22:42:42 +0000161 inFace.getId(), mi.lastNexthop));
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700162
163 return true;
164}
165
166void
Junxiao Shia7f9a292016-11-22 16:31:38 +0000167AccessStrategy::afterRtoTimeout(weak_ptr<pit::Entry> pitWeak, FaceId inFaceId, FaceId firstOutFaceId)
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700168{
169 shared_ptr<pit::Entry> pitEntry = pitWeak.lock();
170 BOOST_ASSERT(pitEntry != nullptr);
Junxiao Shia7f9a292016-11-22 16:31:38 +0000171 // if pitEntry is gone, RTO timer should have been cancelled
172
173 Face* inFace = this->getFace(inFaceId);
174 if (inFace == nullptr) {
175 NFD_LOG_DEBUG(pitEntry->getInterest() << " timeoutFrom " << firstOutFaceId <<
176 " inFace-gone " << inFaceId);
177 return;
178 }
179
180 pit::InRecordCollection::iterator inRecord = pitEntry->getInRecord(*inFace);
181 BOOST_ASSERT(inRecord != pitEntry->in_end());
182 // in-record is erased only if Interest is satisfied, and RTO timer should have been cancelled
183 // note: if this strategy is extended to send Nacks, that would also erase in-record,
184 // and RTO timer should be cancelled in that case as well
185
186 const Interest& interest = inRecord->getInterest();
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700187
Junxiao Shi8d843142016-07-11 22:42:42 +0000188 const fib::Entry& fibEntry = this->lookupFib(*pitEntry);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700189
Junxiao Shia7f9a292016-11-22 16:31:38 +0000190 NFD_LOG_DEBUG(pitEntry->getInterest() << " timeoutFrom " << firstOutFaceId <<
191 " multicast-except " << firstOutFaceId);
192 this->multicast(*inFace, interest, pitEntry, fibEntry, firstOutFaceId);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700193}
194
Junxiao Shia7f9a292016-11-22 16:31:38 +0000195int
196AccessStrategy::multicast(const Face& inFace, const Interest& interest,
197 const shared_ptr<pit::Entry>& pitEntry, const fib::Entry& fibEntry,
198 FaceId exceptFace)
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700199{
Junxiao Shia7f9a292016-11-22 16:31:38 +0000200 int nSent = 0;
Junxiao Shi8d843142016-07-11 22:42:42 +0000201 for (const fib::NextHop& nexthop : fibEntry.getNextHops()) {
Junxiao Shia7f9a292016-11-22 16:31:38 +0000202 Face& outFace = nexthop.getFace();
203 if (&outFace == &inFace || outFace.getId() == exceptFace ||
204 wouldViolateScope(inFace, interest, outFace)) {
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700205 continue;
206 }
Junxiao Shia7f9a292016-11-22 16:31:38 +0000207 NFD_LOG_DEBUG(pitEntry->getInterest() << " interestTo " << outFace.getId() <<
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700208 " multicast");
Junxiao Shia7f9a292016-11-22 16:31:38 +0000209 this->sendInterest(pitEntry, outFace, interest);
210 ++nSent;
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700211 }
Junxiao Shia7f9a292016-11-22 16:31:38 +0000212 return nSent;
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700213}
214
215void
Junxiao Shi15e98b02016-08-12 11:21:44 +0000216AccessStrategy::beforeSatisfyInterest(const shared_ptr<pit::Entry>& pitEntry,
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700217 const Face& inFace, const Data& data)
218{
Junxiao Shifc021862016-08-25 21:51:18 +0000219 PitInfo* pi = pitEntry->getStrategyInfo<PitInfo>();
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700220 if (pi != nullptr) {
221 pi->rtoTimer.cancel();
222 }
223
Junxiao Shi4846f372016-04-05 13:39:30 -0700224 if (!pitEntry->hasInRecords()) { // already satisfied by another upstream
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700225 NFD_LOG_DEBUG(pitEntry->getInterest() << " dataFrom " << inFace.getId() <<
226 " not-fastest");
227 return;
228 }
229
Junxiao Shi4846f372016-04-05 13:39:30 -0700230 pit::OutRecordCollection::iterator outRecord = pitEntry->getOutRecord(inFace);
231 if (outRecord == pitEntry->out_end()) { // no out-record
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700232 NFD_LOG_DEBUG(pitEntry->getInterest() << " dataFrom " << inFace.getId() <<
233 " no-out-record");
234 return;
235 }
236
237 time::steady_clock::Duration rtt = time::steady_clock::now() - outRecord->getLastRenewed();
238 NFD_LOG_DEBUG(pitEntry->getInterest() << " dataFrom " << inFace.getId() <<
239 " rtt=" << time::duration_cast<time::microseconds>(rtt).count());
240 this->updateMeasurements(inFace, data, time::duration_cast<RttEstimator::Duration>(rtt));
241}
242
243void
244AccessStrategy::updateMeasurements(const Face& inFace, const Data& data,
245 const RttEstimator::Duration& rtt)
246{
Junxiao Shif15058a2016-12-11 20:15:05 +0000247 ///\todo move FaceInfoTable out of AccessStrategy instance, to Measurements or somewhere else
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700248 FaceInfo& fi = m_fit[inFace.getId()];
249 fi.rtt.addMeasurement(rtt);
250
Junxiao Shifc021862016-08-25 21:51:18 +0000251 MtInfo* mi = this->addPrefixMeasurements(data);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700252 if (mi->lastNexthop != inFace.getId()) {
253 mi->lastNexthop = inFace.getId();
254 mi->rtt = fi.rtt;
255 }
256 else {
257 mi->rtt.addMeasurement(rtt);
258 }
259}
260
261AccessStrategy::MtInfo::MtInfo()
Junxiao Shicde37ad2015-12-24 01:02:05 -0700262 : lastNexthop(face::INVALID_FACEID)
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700263 , rtt(1, time::milliseconds(1), 0.1)
264{
265}
266
Junxiao Shifc021862016-08-25 21:51:18 +0000267std::tuple<Name, AccessStrategy::MtInfo*>
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700268AccessStrategy::findPrefixMeasurements(const pit::Entry& pitEntry)
269{
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000270 measurements::Entry* me = this->getMeasurements().findLongestPrefixMatch(pitEntry);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700271 if (me == nullptr) {
Junxiao Shi5b3feb62016-08-19 01:51:41 +0000272 return std::make_tuple(Name(), nullptr);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700273 }
274
Junxiao Shifc021862016-08-25 21:51:18 +0000275 MtInfo* mi = me->getStrategyInfo<MtInfo>();
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700276 BOOST_ASSERT(mi != nullptr);
277 // XXX after runtime strategy change, it's possible that me exists but mi doesn't exist;
278 // this case needs another longest prefix match until mi is found
Junxiao Shi5b3feb62016-08-19 01:51:41 +0000279 return std::make_tuple(me->getName(), mi);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700280}
281
Junxiao Shifc021862016-08-25 21:51:18 +0000282AccessStrategy::MtInfo*
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700283AccessStrategy::addPrefixMeasurements(const Data& data)
284{
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000285 measurements::Entry* me = nullptr;
286 if (!data.getName().empty()) {
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700287 me = this->getMeasurements().get(data.getName().getPrefix(-1));
288 }
289 if (me == nullptr) { // parent of Data Name is not in this strategy, or Data Name is empty
290 me = this->getMeasurements().get(data.getName());
291 // Data Name must be in this strategy
292 BOOST_ASSERT(me != nullptr);
293 }
294
Junxiao Shi7b0f4d12015-05-10 21:40:29 -0700295 static const time::nanoseconds ME_LIFETIME = time::seconds(8);
296 this->getMeasurements().extendLifetime(*me, ME_LIFETIME);
297
Junxiao Shifc021862016-08-25 21:51:18 +0000298 return me->insertStrategyInfo<MtInfo>().first;
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700299}
300
301AccessStrategy::FaceInfo::FaceInfo()
302 : rtt(1, time::milliseconds(1), 0.1)
303{
304}
305
306void
Junxiao Shiae04d342016-07-19 13:20:22 +0000307AccessStrategy::removeFaceInfo(const Face& face)
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700308{
Junxiao Shiae04d342016-07-19 13:20:22 +0000309 m_fit.erase(face.getId());
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700310}
311
312} // namespace fw
313} // namespace nfd