blob: 3b282462b81e5193638dc73102434d57ff19677c [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/*
ashiqopu3ad49db2018-10-20 22:38:47 +00003 * Copyright (c) 2014-2019, 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"
Davide Pesavento3dade002019-03-19 11:29:56 -060029#include "daemon/global.hpp"
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070030
31namespace nfd {
32namespace fw {
33
Davide Pesaventoa3148082018-04-12 18:21:54 -040034NFD_LOG_INIT(AccessStrategy);
Junxiao Shifaf3eb02015-02-16 10:50:36 -070035NFD_REGISTER_STRATEGY(AccessStrategy);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070036
37AccessStrategy::AccessStrategy(Forwarder& forwarder, const Name& name)
Junxiao Shi18739c42016-12-22 08:03:00 +000038 : Strategy(forwarder)
Davide Pesaventoe4b22382018-06-10 14:37:24 -040039 , m_removeFaceInfoConn(beforeRemoveFace.connect([this] (const Face& face) { removeFaceInfo(face); }))
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070040{
Junxiao Shi18739c42016-12-22 08:03:00 +000041 ParsedInstanceName parsed = parseInstanceName(name);
42 if (!parsed.parameters.empty()) {
Davide Pesavento19779d82019-02-14 13:40:04 -050043 NDN_THROW(std::invalid_argument("AccessStrategy does not accept parameters"));
Junxiao Shi18739c42016-12-22 08:03:00 +000044 }
Junxiao Shi91f6ee02016-12-29 21:44:44 +000045 if (parsed.version && *parsed.version != getStrategyName()[-1].toVersion()) {
Davide Pesavento3dade002019-03-19 11:29:56 -060046 NDN_THROW(std::invalid_argument("AccessStrategy does not support version " + to_string(*parsed.version)));
Junxiao Shi91f6ee02016-12-29 21:44:44 +000047 }
Junxiao Shi18739c42016-12-22 08:03:00 +000048 this->setInstanceName(makeInstanceName(name, getStrategyName()));
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070049}
50
Junxiao Shi037f4ab2016-12-13 04:27:06 +000051const Name&
52AccessStrategy::getStrategyName()
53{
54 static Name strategyName("/localhost/nfd/strategy/access/%FD%01");
55 return strategyName;
56}
57
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070058void
ashiqopuc7079482019-02-20 05:34:37 +000059AccessStrategy::afterReceiveInterest(const FaceEndpoint& ingress, const Interest& interest,
Junxiao Shi15e98b02016-08-12 11:21:44 +000060 const shared_ptr<pit::Entry>& pitEntry)
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070061{
Davide Pesavento17c172b2019-03-23 15:11:44 -040062 auto suppressResult = m_retxSuppression.decidePerPitEntry(*pitEntry);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070063 switch (suppressResult) {
Ashlesh Gawande90015992017-07-11 17:25:48 -050064 case RetxSuppressionResult::NEW:
Davide Pesavento17c172b2019-03-23 15:11:44 -040065 return afterReceiveNewInterest(ingress, interest, pitEntry);
Ashlesh Gawande90015992017-07-11 17:25:48 -050066 case RetxSuppressionResult::FORWARD:
Davide Pesavento17c172b2019-03-23 15:11:44 -040067 return afterReceiveRetxInterest(ingress, interest, pitEntry);
Ashlesh Gawande90015992017-07-11 17:25:48 -050068 case RetxSuppressionResult::SUPPRESS:
ashiqopuc7079482019-02-20 05:34:37 +000069 NFD_LOG_DEBUG(interest << " interestFrom " << ingress << " retx-suppress");
Davide Pesavento17c172b2019-03-23 15:11:44 -040070 return;
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070071 }
72}
73
74void
Davide Pesavento17c172b2019-03-23 15:11:44 -040075AccessStrategy::afterReceiveNewInterest(const FaceEndpoint& ingress, const Interest& interest,
Junxiao Shi15e98b02016-08-12 11:21:44 +000076 const shared_ptr<pit::Entry>& pitEntry)
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070077{
Davide Pesavento17c172b2019-03-23 15:11:44 -040078 const auto& fibEntry = this->lookupFib(*pitEntry);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070079 Name miName;
Junxiao Shifc021862016-08-25 21:51:18 +000080 MtInfo* mi = nullptr;
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070081 std::tie(miName, mi) = this->findPrefixMeasurements(*pitEntry);
82
83 // has measurements for Interest Name?
84 if (mi != nullptr) {
Davide Pesavento17c172b2019-03-23 15:11:44 -040085 NFD_LOG_DEBUG(interest << " interestFrom " << ingress << " new-interest mi=" << miName);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070086
87 // send to last working nexthop
Davide Pesavento17c172b2019-03-23 15:11:44 -040088 bool isSentToLastNexthop = this->sendToLastNexthop(ingress, interest, pitEntry, *mi, fibEntry);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070089 if (isSentToLastNexthop) {
90 return;
91 }
92 }
93 else {
Davide Pesavento17c172b2019-03-23 15:11:44 -040094 NFD_LOG_DEBUG(interest << " interestFrom " << ingress << " new-interest no-mi");
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070095 }
96
97 // no measurements, or last working nexthop unavailable
98
Junxiao Shi965d3a42015-06-01 06:55:23 -070099 // multicast to all nexthops except incoming face
Davide Pesavento17c172b2019-03-23 15:11:44 -0400100 size_t nMulticastSent = this->multicast(ingress.face, interest, pitEntry, fibEntry);
Junxiao Shia7f9a292016-11-22 16:31:38 +0000101
Davide Pesavento17c172b2019-03-23 15:11:44 -0400102 if (nMulticastSent == 0) {
Junxiao Shia7f9a292016-11-22 16:31:38 +0000103 this->rejectPendingInterest(pitEntry);
104 }
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700105}
106
107void
Davide Pesavento17c172b2019-03-23 15:11:44 -0400108AccessStrategy::afterReceiveRetxInterest(const FaceEndpoint& ingress, const Interest& interest,
Junxiao Shi15e98b02016-08-12 11:21:44 +0000109 const shared_ptr<pit::Entry>& pitEntry)
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700110{
Davide Pesavento17c172b2019-03-23 15:11:44 -0400111 const auto& fibEntry = this->lookupFib(*pitEntry);
112 NFD_LOG_DEBUG(interest << " interestFrom " << ingress << " retx-forward");
113 this->multicast(ingress.face, interest, pitEntry, fibEntry);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700114}
115
116bool
Davide Pesavento17c172b2019-03-23 15:11:44 -0400117AccessStrategy::sendToLastNexthop(const FaceEndpoint& ingress, const Interest& interest,
Junxiao Shia7f9a292016-11-22 16:31:38 +0000118 const shared_ptr<pit::Entry>& pitEntry, MtInfo& mi,
119 const fib::Entry& fibEntry)
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700120{
Junxiao Shicde37ad2015-12-24 01:02:05 -0700121 if (mi.lastNexthop == face::INVALID_FACEID) {
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700122 NFD_LOG_DEBUG(pitEntry->getInterest() << " no-last-nexthop");
123 return false;
124 }
125
Davide Pesavento17c172b2019-03-23 15:11:44 -0400126 if (mi.lastNexthop == ingress.face.getId()) {
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700127 NFD_LOG_DEBUG(pitEntry->getInterest() << " last-nexthop-is-downstream");
128 return false;
129 }
130
Junxiao Shia7f9a292016-11-22 16:31:38 +0000131 Face* outFace = this->getFace(mi.lastNexthop);
ashiqopu3ad49db2018-10-20 22:38:47 +0000132 if (outFace == nullptr || !fibEntry.hasNextHop(*outFace, 0)) {
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700133 NFD_LOG_DEBUG(pitEntry->getInterest() << " last-nexthop-gone");
134 return false;
135 }
136
Davide Pesavento17c172b2019-03-23 15:11:44 -0400137 if (wouldViolateScope(ingress.face, interest, *outFace)) {
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700138 NFD_LOG_DEBUG(pitEntry->getInterest() << " last-nexthop-violates-scope");
139 return false;
140 }
141
Davide Pesavento17c172b2019-03-23 15:11:44 -0400142 auto rto = mi.rtt.computeRto();
ashiqopuc7079482019-02-20 05:34:37 +0000143 NFD_LOG_DEBUG(pitEntry->getInterest() << " interestTo " << mi.lastNexthop
144 << " last-nexthop rto=" << time::duration_cast<time::microseconds>(rto).count());
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700145
ashiqopuc7079482019-02-20 05:34:37 +0000146 this->sendInterest(pitEntry, FaceEndpoint(*outFace, 0), interest);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700147
148 // schedule RTO timeout
Junxiao Shifc021862016-08-25 21:51:18 +0000149 PitInfo* pi = pitEntry->insertStrategyInfo<PitInfo>().first;
Davide Pesavento17c172b2019-03-23 15:11:44 -0400150 pi->rtoTimer = getScheduler().schedule(rto, [=] {
151 afterRtoTimeout(weak_ptr<pit::Entry>(pitEntry),
152 ingress.face.getId(), ingress.endpoint, mi.lastNexthop);
153 });
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700154
155 return true;
156}
157
158void
Davide Pesavento17c172b2019-03-23 15:11:44 -0400159AccessStrategy::afterRtoTimeout(const weak_ptr<pit::Entry>& pitWeak,
160 FaceId inFaceId, EndpointId inEndpointId, FaceId firstOutFaceId)
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700161{
162 shared_ptr<pit::Entry> pitEntry = pitWeak.lock();
Davide Pesavento17c172b2019-03-23 15:11:44 -0400163 // if PIT entry is gone, RTO timer should have been cancelled
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700164 BOOST_ASSERT(pitEntry != nullptr);
Junxiao Shia7f9a292016-11-22 16:31:38 +0000165
166 Face* inFace = this->getFace(inFaceId);
167 if (inFace == nullptr) {
ashiqopuc7079482019-02-20 05:34:37 +0000168 NFD_LOG_DEBUG(pitEntry->getInterest() << " timeoutFrom " << firstOutFaceId
169 << " inFace-gone " << inFaceId);
Junxiao Shia7f9a292016-11-22 16:31:38 +0000170 return;
171 }
172
Davide Pesavento17c172b2019-03-23 15:11:44 -0400173 auto inRecord = pitEntry->getInRecord(*inFace, inEndpointId);
Junxiao Shia7f9a292016-11-22 16:31:38 +0000174 // in-record is erased only if Interest is satisfied, and RTO timer should have been cancelled
Davide Pesavento17c172b2019-03-23 15:11:44 -0400175 // note: if this strategy is extended to send Nacks, that would also erase the in-record,
176 // and the RTO timer should be cancelled in that case as well
177 BOOST_ASSERT(inRecord != pitEntry->in_end());
Junxiao Shia7f9a292016-11-22 16:31:38 +0000178
179 const Interest& interest = inRecord->getInterest();
Junxiao Shi8d843142016-07-11 22:42:42 +0000180 const fib::Entry& fibEntry = this->lookupFib(*pitEntry);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700181
ashiqopuc7079482019-02-20 05:34:37 +0000182 NFD_LOG_DEBUG(pitEntry->getInterest() << " timeoutFrom " << firstOutFaceId
183 << " multicast-except " << firstOutFaceId);
Junxiao Shia7f9a292016-11-22 16:31:38 +0000184 this->multicast(*inFace, interest, pitEntry, fibEntry, firstOutFaceId);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700185}
186
Davide Pesavento17c172b2019-03-23 15:11:44 -0400187size_t
Junxiao Shia7f9a292016-11-22 16:31:38 +0000188AccessStrategy::multicast(const Face& inFace, const Interest& interest,
189 const shared_ptr<pit::Entry>& pitEntry, const fib::Entry& fibEntry,
190 FaceId exceptFace)
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700191{
Davide Pesavento17c172b2019-03-23 15:11:44 -0400192 size_t nSent = 0;
193 for (const auto& nexthop : fibEntry.getNextHops()) {
Junxiao Shia7f9a292016-11-22 16:31:38 +0000194 Face& outFace = nexthop.getFace();
195 if (&outFace == &inFace || outFace.getId() == exceptFace ||
196 wouldViolateScope(inFace, interest, outFace)) {
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700197 continue;
198 }
ashiqopuc7079482019-02-20 05:34:37 +0000199 NFD_LOG_DEBUG(pitEntry->getInterest() << " interestTo " << outFace.getId() << " multicast");
200 this->sendInterest(pitEntry, FaceEndpoint(outFace, 0), interest);
Junxiao Shia7f9a292016-11-22 16:31:38 +0000201 ++nSent;
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700202 }
Junxiao Shia7f9a292016-11-22 16:31:38 +0000203 return nSent;
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700204}
205
206void
Junxiao Shi15e98b02016-08-12 11:21:44 +0000207AccessStrategy::beforeSatisfyInterest(const shared_ptr<pit::Entry>& pitEntry,
ashiqopuc7079482019-02-20 05:34:37 +0000208 const FaceEndpoint& ingress, const Data& data)
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700209{
Junxiao Shifc021862016-08-25 21:51:18 +0000210 PitInfo* pi = pitEntry->getStrategyInfo<PitInfo>();
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700211 if (pi != nullptr) {
212 pi->rtoTimer.cancel();
213 }
214
Junxiao Shi4846f372016-04-05 13:39:30 -0700215 if (!pitEntry->hasInRecords()) { // already satisfied by another upstream
ashiqopuc7079482019-02-20 05:34:37 +0000216 NFD_LOG_DEBUG(pitEntry->getInterest() << " dataFrom " << ingress << " not-fastest");
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700217 return;
218 }
219
ashiqopuc7079482019-02-20 05:34:37 +0000220 auto outRecord = pitEntry->getOutRecord(ingress.face, 0);
Junxiao Shi4846f372016-04-05 13:39:30 -0700221 if (outRecord == pitEntry->out_end()) { // no out-record
ashiqopuc7079482019-02-20 05:34:37 +0000222 NFD_LOG_DEBUG(pitEntry->getInterest() << " dataFrom " << ingress << " no-out-record");
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700223 return;
224 }
225
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400226 auto rtt = time::steady_clock::now() - outRecord->getLastRenewed();
ashiqopuc7079482019-02-20 05:34:37 +0000227 NFD_LOG_DEBUG(pitEntry->getInterest() << " dataFrom " << ingress
228 << " rtt=" << time::duration_cast<time::microseconds>(rtt).count());
229 this->updateMeasurements(ingress.face, data, time::duration_cast<RttEstimator::Duration>(rtt));
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700230}
231
232void
233AccessStrategy::updateMeasurements(const Face& inFace, const Data& data,
234 const RttEstimator::Duration& rtt)
235{
Junxiao Shif15058a2016-12-11 20:15:05 +0000236 ///\todo move FaceInfoTable out of AccessStrategy instance, to Measurements or somewhere else
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700237 FaceInfo& fi = m_fit[inFace.getId()];
238 fi.rtt.addMeasurement(rtt);
239
Junxiao Shifc021862016-08-25 21:51:18 +0000240 MtInfo* mi = this->addPrefixMeasurements(data);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700241 if (mi->lastNexthop != inFace.getId()) {
242 mi->lastNexthop = inFace.getId();
243 mi->rtt = fi.rtt;
244 }
245 else {
246 mi->rtt.addMeasurement(rtt);
247 }
248}
249
Junxiao Shifc021862016-08-25 21:51:18 +0000250std::tuple<Name, AccessStrategy::MtInfo*>
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700251AccessStrategy::findPrefixMeasurements(const pit::Entry& pitEntry)
252{
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000253 measurements::Entry* me = this->getMeasurements().findLongestPrefixMatch(pitEntry);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700254 if (me == nullptr) {
Junxiao Shi5b3feb62016-08-19 01:51:41 +0000255 return std::make_tuple(Name(), nullptr);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700256 }
257
Junxiao Shifc021862016-08-25 21:51:18 +0000258 MtInfo* mi = me->getStrategyInfo<MtInfo>();
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700259 BOOST_ASSERT(mi != nullptr);
260 // XXX after runtime strategy change, it's possible that me exists but mi doesn't exist;
261 // this case needs another longest prefix match until mi is found
Junxiao Shi5b3feb62016-08-19 01:51:41 +0000262 return std::make_tuple(me->getName(), mi);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700263}
264
Junxiao Shifc021862016-08-25 21:51:18 +0000265AccessStrategy::MtInfo*
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700266AccessStrategy::addPrefixMeasurements(const Data& data)
267{
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000268 measurements::Entry* me = nullptr;
269 if (!data.getName().empty()) {
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700270 me = this->getMeasurements().get(data.getName().getPrefix(-1));
271 }
272 if (me == nullptr) { // parent of Data Name is not in this strategy, or Data Name is empty
273 me = this->getMeasurements().get(data.getName());
274 // Data Name must be in this strategy
275 BOOST_ASSERT(me != nullptr);
276 }
277
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400278 this->getMeasurements().extendLifetime(*me, 8_s);
Junxiao Shi7b0f4d12015-05-10 21:40:29 -0700279
Junxiao Shifc021862016-08-25 21:51:18 +0000280 return me->insertStrategyInfo<MtInfo>().first;
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700281}
282
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700283void
Junxiao Shiae04d342016-07-19 13:20:22 +0000284AccessStrategy::removeFaceInfo(const Face& face)
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700285{
Junxiao Shiae04d342016-07-19 13:20:22 +0000286 m_fit.erase(face.getId());
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700287}
288
289} // namespace fw
290} // namespace nfd