blob: e7e09041ed748f024e2c74ac65129ae703c77a35 [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"
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)
Davide Pesaventoe4b22382018-06-10 14:37:24 -040038 , m_removeFaceInfoConn(beforeRemoveFace.connect([this] (const Face& face) { removeFaceInfo(face); }))
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070039{
Junxiao Shi18739c42016-12-22 08:03:00 +000040 ParsedInstanceName parsed = parseInstanceName(name);
41 if (!parsed.parameters.empty()) {
Davide Pesavento19779d82019-02-14 13:40:04 -050042 NDN_THROW(std::invalid_argument("AccessStrategy does not accept parameters"));
Junxiao Shi18739c42016-12-22 08:03:00 +000043 }
Junxiao Shi91f6ee02016-12-29 21:44:44 +000044 if (parsed.version && *parsed.version != getStrategyName()[-1].toVersion()) {
Davide Pesavento19779d82019-02-14 13:40:04 -050045 NDN_THROW(std::invalid_argument(
Alexander Afanasyev0c63c632017-12-05 11:17:09 -050046 "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{
Ashlesh Gawande90015992017-07-11 17:25:48 -050062 RetxSuppressionResult 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:
ashiqopuc7079482019-02-20 05:34:37 +000065 this->afterReceiveNewInterest(ingress.face, interest, pitEntry);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070066 break;
Ashlesh Gawande90015992017-07-11 17:25:48 -050067 case RetxSuppressionResult::FORWARD:
ashiqopuc7079482019-02-20 05:34:37 +000068 this->afterReceiveRetxInterest(ingress.face, interest, pitEntry);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070069 break;
Ashlesh Gawande90015992017-07-11 17:25:48 -050070 case RetxSuppressionResult::SUPPRESS:
ashiqopuc7079482019-02-20 05:34:37 +000071 NFD_LOG_DEBUG(interest << " interestFrom " << ingress << " retx-suppress");
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070072 break;
73 default:
74 BOOST_ASSERT(false);
75 break;
76 }
77}
78
79void
Junxiao Shi15e98b02016-08-12 11:21:44 +000080AccessStrategy::afterReceiveNewInterest(const Face& inFace, const Interest& interest,
81 const shared_ptr<pit::Entry>& pitEntry)
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070082{
Junxiao Shi8d843142016-07-11 22:42:42 +000083 const fib::Entry& fibEntry = this->lookupFib(*pitEntry);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070084 Name miName;
Junxiao Shifc021862016-08-25 21:51:18 +000085 MtInfo* mi = nullptr;
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070086 std::tie(miName, mi) = this->findPrefixMeasurements(*pitEntry);
87
88 // has measurements for Interest Name?
89 if (mi != nullptr) {
ashiqopuc7079482019-02-20 05:34:37 +000090 NFD_LOG_DEBUG(interest << " interestFrom " << inFace.getId() << " new-interest mi=" << miName);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070091
92 // send to last working nexthop
Junxiao Shia7f9a292016-11-22 16:31:38 +000093 bool isSentToLastNexthop = this->sendToLastNexthop(inFace, interest, pitEntry, *mi, fibEntry);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070094
95 if (isSentToLastNexthop) {
96 return;
97 }
98 }
99 else {
ashiqopuc7079482019-02-20 05:34:37 +0000100 NFD_LOG_DEBUG(interest << " interestFrom " << inFace.getId() << " new-interest no-mi");
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700101 }
102
103 // no measurements, or last working nexthop unavailable
104
Junxiao Shi965d3a42015-06-01 06:55:23 -0700105 // multicast to all nexthops except incoming face
Junxiao Shia7f9a292016-11-22 16:31:38 +0000106 int nMulticastSent = this->multicast(inFace, interest, pitEntry, fibEntry);
107
108 if (nMulticastSent < 1) {
109 this->rejectPendingInterest(pitEntry);
110 }
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700111}
112
113void
Junxiao Shi15e98b02016-08-12 11:21:44 +0000114AccessStrategy::afterReceiveRetxInterest(const Face& inFace, const Interest& interest,
115 const shared_ptr<pit::Entry>& pitEntry)
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700116{
Junxiao Shi8d843142016-07-11 22:42:42 +0000117 const fib::Entry& fibEntry = this->lookupFib(*pitEntry);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700118 NFD_LOG_DEBUG(interest << " interestFrom " << inFace.getId() << " retx-forward");
Junxiao Shia7f9a292016-11-22 16:31:38 +0000119 this->multicast(inFace, interest, pitEntry, fibEntry);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700120}
121
122bool
Junxiao Shia7f9a292016-11-22 16:31:38 +0000123AccessStrategy::sendToLastNexthop(const Face& inFace, const Interest& interest,
124 const shared_ptr<pit::Entry>& pitEntry, MtInfo& mi,
125 const fib::Entry& fibEntry)
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700126{
Junxiao Shicde37ad2015-12-24 01:02:05 -0700127 if (mi.lastNexthop == face::INVALID_FACEID) {
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700128 NFD_LOG_DEBUG(pitEntry->getInterest() << " no-last-nexthop");
129 return false;
130 }
131
132 if (mi.lastNexthop == inFace.getId()) {
133 NFD_LOG_DEBUG(pitEntry->getInterest() << " last-nexthop-is-downstream");
134 return false;
135 }
136
Junxiao Shia7f9a292016-11-22 16:31:38 +0000137 Face* outFace = this->getFace(mi.lastNexthop);
ashiqopu3ad49db2018-10-20 22:38:47 +0000138 if (outFace == nullptr || !fibEntry.hasNextHop(*outFace, 0)) {
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700139 NFD_LOG_DEBUG(pitEntry->getInterest() << " last-nexthop-gone");
140 return false;
141 }
142
Junxiao Shia7f9a292016-11-22 16:31:38 +0000143 if (wouldViolateScope(inFace, interest, *outFace)) {
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700144 NFD_LOG_DEBUG(pitEntry->getInterest() << " last-nexthop-violates-scope");
145 return false;
146 }
147
148 RttEstimator::Duration rto = mi.rtt.computeRto();
ashiqopuc7079482019-02-20 05:34:37 +0000149 NFD_LOG_DEBUG(pitEntry->getInterest() << " interestTo " << mi.lastNexthop
150 << " last-nexthop rto=" << time::duration_cast<time::microseconds>(rto).count());
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700151
ashiqopuc7079482019-02-20 05:34:37 +0000152 this->sendInterest(pitEntry, FaceEndpoint(*outFace, 0), interest);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700153
154 // schedule RTO timeout
Junxiao Shifc021862016-08-25 21:51:18 +0000155 PitInfo* pi = pitEntry->insertStrategyInfo<PitInfo>().first;
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700156 pi->rtoTimer = scheduler::schedule(rto,
157 bind(&AccessStrategy::afterRtoTimeout, this, weak_ptr<pit::Entry>(pitEntry),
Junxiao Shi8d843142016-07-11 22:42:42 +0000158 inFace.getId(), mi.lastNexthop));
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700159
160 return true;
161}
162
163void
Junxiao Shia7f9a292016-11-22 16:31:38 +0000164AccessStrategy::afterRtoTimeout(weak_ptr<pit::Entry> pitWeak, FaceId inFaceId, FaceId firstOutFaceId)
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700165{
166 shared_ptr<pit::Entry> pitEntry = pitWeak.lock();
167 BOOST_ASSERT(pitEntry != nullptr);
Junxiao Shia7f9a292016-11-22 16:31:38 +0000168 // if pitEntry is gone, RTO timer should have been cancelled
169
170 Face* inFace = this->getFace(inFaceId);
171 if (inFace == nullptr) {
ashiqopuc7079482019-02-20 05:34:37 +0000172 NFD_LOG_DEBUG(pitEntry->getInterest() << " timeoutFrom " << firstOutFaceId
173 << " inFace-gone " << inFaceId);
Junxiao Shia7f9a292016-11-22 16:31:38 +0000174 return;
175 }
176
ashiqopud3ae85d2019-02-17 02:29:55 +0000177 auto inRecord = pitEntry->getInRecord(*inFace, 0);
Junxiao Shia7f9a292016-11-22 16:31:38 +0000178 BOOST_ASSERT(inRecord != pitEntry->in_end());
179 // in-record is erased only if Interest is satisfied, and RTO timer should have been cancelled
180 // note: if this strategy is extended to send Nacks, that would also erase in-record,
181 // and RTO timer should be cancelled in that case as well
182
183 const Interest& interest = inRecord->getInterest();
Junxiao Shi8d843142016-07-11 22:42:42 +0000184 const fib::Entry& fibEntry = this->lookupFib(*pitEntry);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700185
ashiqopuc7079482019-02-20 05:34:37 +0000186 NFD_LOG_DEBUG(pitEntry->getInterest() << " timeoutFrom " << firstOutFaceId
187 << " multicast-except " << firstOutFaceId);
Junxiao Shia7f9a292016-11-22 16:31:38 +0000188 this->multicast(*inFace, interest, pitEntry, fibEntry, firstOutFaceId);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700189}
190
Junxiao Shia7f9a292016-11-22 16:31:38 +0000191int
192AccessStrategy::multicast(const Face& inFace, const Interest& interest,
193 const shared_ptr<pit::Entry>& pitEntry, const fib::Entry& fibEntry,
194 FaceId exceptFace)
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700195{
Junxiao Shia7f9a292016-11-22 16:31:38 +0000196 int nSent = 0;
Junxiao Shi8d843142016-07-11 22:42:42 +0000197 for (const fib::NextHop& nexthop : fibEntry.getNextHops()) {
Junxiao Shia7f9a292016-11-22 16:31:38 +0000198 Face& outFace = nexthop.getFace();
199 if (&outFace == &inFace || outFace.getId() == exceptFace ||
200 wouldViolateScope(inFace, interest, outFace)) {
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700201 continue;
202 }
ashiqopuc7079482019-02-20 05:34:37 +0000203 NFD_LOG_DEBUG(pitEntry->getInterest() << " interestTo " << outFace.getId() << " multicast");
204 this->sendInterest(pitEntry, FaceEndpoint(outFace, 0), interest);
Junxiao Shia7f9a292016-11-22 16:31:38 +0000205 ++nSent;
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700206 }
Junxiao Shia7f9a292016-11-22 16:31:38 +0000207 return nSent;
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700208}
209
210void
Junxiao Shi15e98b02016-08-12 11:21:44 +0000211AccessStrategy::beforeSatisfyInterest(const shared_ptr<pit::Entry>& pitEntry,
ashiqopuc7079482019-02-20 05:34:37 +0000212 const FaceEndpoint& ingress, const Data& data)
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700213{
Junxiao Shifc021862016-08-25 21:51:18 +0000214 PitInfo* pi = pitEntry->getStrategyInfo<PitInfo>();
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700215 if (pi != nullptr) {
216 pi->rtoTimer.cancel();
217 }
218
Junxiao Shi4846f372016-04-05 13:39:30 -0700219 if (!pitEntry->hasInRecords()) { // already satisfied by another upstream
ashiqopuc7079482019-02-20 05:34:37 +0000220 NFD_LOG_DEBUG(pitEntry->getInterest() << " dataFrom " << ingress << " not-fastest");
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700221 return;
222 }
223
ashiqopuc7079482019-02-20 05:34:37 +0000224 auto outRecord = pitEntry->getOutRecord(ingress.face, 0);
Junxiao Shi4846f372016-04-05 13:39:30 -0700225 if (outRecord == pitEntry->out_end()) { // no out-record
ashiqopuc7079482019-02-20 05:34:37 +0000226 NFD_LOG_DEBUG(pitEntry->getInterest() << " dataFrom " << ingress << " no-out-record");
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700227 return;
228 }
229
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400230 auto rtt = time::steady_clock::now() - outRecord->getLastRenewed();
ashiqopuc7079482019-02-20 05:34:37 +0000231 NFD_LOG_DEBUG(pitEntry->getInterest() << " dataFrom " << ingress
232 << " rtt=" << time::duration_cast<time::microseconds>(rtt).count());
233 this->updateMeasurements(ingress.face, data, time::duration_cast<RttEstimator::Duration>(rtt));
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700234}
235
236void
237AccessStrategy::updateMeasurements(const Face& inFace, const Data& data,
238 const RttEstimator::Duration& rtt)
239{
Junxiao Shif15058a2016-12-11 20:15:05 +0000240 ///\todo move FaceInfoTable out of AccessStrategy instance, to Measurements or somewhere else
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700241 FaceInfo& fi = m_fit[inFace.getId()];
242 fi.rtt.addMeasurement(rtt);
243
Junxiao Shifc021862016-08-25 21:51:18 +0000244 MtInfo* mi = this->addPrefixMeasurements(data);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700245 if (mi->lastNexthop != inFace.getId()) {
246 mi->lastNexthop = inFace.getId();
247 mi->rtt = fi.rtt;
248 }
249 else {
250 mi->rtt.addMeasurement(rtt);
251 }
252}
253
254AccessStrategy::MtInfo::MtInfo()
Junxiao Shicde37ad2015-12-24 01:02:05 -0700255 : lastNexthop(face::INVALID_FACEID)
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400256 , rtt(1, 1_ms, 0.1)
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700257{
258}
259
Junxiao Shifc021862016-08-25 21:51:18 +0000260std::tuple<Name, AccessStrategy::MtInfo*>
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700261AccessStrategy::findPrefixMeasurements(const pit::Entry& pitEntry)
262{
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000263 measurements::Entry* me = this->getMeasurements().findLongestPrefixMatch(pitEntry);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700264 if (me == nullptr) {
Junxiao Shi5b3feb62016-08-19 01:51:41 +0000265 return std::make_tuple(Name(), nullptr);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700266 }
267
Junxiao Shifc021862016-08-25 21:51:18 +0000268 MtInfo* mi = me->getStrategyInfo<MtInfo>();
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700269 BOOST_ASSERT(mi != nullptr);
270 // XXX after runtime strategy change, it's possible that me exists but mi doesn't exist;
271 // this case needs another longest prefix match until mi is found
Junxiao Shi5b3feb62016-08-19 01:51:41 +0000272 return std::make_tuple(me->getName(), mi);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700273}
274
Junxiao Shifc021862016-08-25 21:51:18 +0000275AccessStrategy::MtInfo*
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700276AccessStrategy::addPrefixMeasurements(const Data& data)
277{
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000278 measurements::Entry* me = nullptr;
279 if (!data.getName().empty()) {
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700280 me = this->getMeasurements().get(data.getName().getPrefix(-1));
281 }
282 if (me == nullptr) { // parent of Data Name is not in this strategy, or Data Name is empty
283 me = this->getMeasurements().get(data.getName());
284 // Data Name must be in this strategy
285 BOOST_ASSERT(me != nullptr);
286 }
287
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400288 this->getMeasurements().extendLifetime(*me, 8_s);
Junxiao Shi7b0f4d12015-05-10 21:40:29 -0700289
Junxiao Shifc021862016-08-25 21:51:18 +0000290 return me->insertStrategyInfo<MtInfo>().first;
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700291}
292
293AccessStrategy::FaceInfo::FaceInfo()
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400294 : rtt(1, 1_ms, 0.1)
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700295{
296}
297
298void
Junxiao Shiae04d342016-07-19 13:20:22 +0000299AccessStrategy::removeFaceInfo(const Face& face)
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700300{
Junxiao Shiae04d342016-07-19 13:20:22 +0000301 m_fit.erase(face.getId());
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700302}
303
304} // namespace fw
305} // namespace nfd