blob: 3705037d335ad28d5fbc3e5b5849806587f992d1 [file] [log] [blame]
Junxiao Shi80ee7cb2014-12-14 10:53:05 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shifef73e42016-03-29 14:15:05 -07003 * Copyright (c) 2014-2016, 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
33NFD_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 }
45 this->setInstanceName(makeInstanceName(name, getStrategyName()));
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070046}
47
Junxiao Shi037f4ab2016-12-13 04:27:06 +000048const Name&
49AccessStrategy::getStrategyName()
50{
51 static Name strategyName("/localhost/nfd/strategy/access/%FD%01");
52 return strategyName;
53}
54
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070055void
Junxiao Shi15e98b02016-08-12 11:21:44 +000056AccessStrategy::afterReceiveInterest(const Face& inFace, const Interest& interest,
57 const shared_ptr<pit::Entry>& pitEntry)
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070058{
Junxiao Shia788e252015-01-28 17:06:25 -070059 RetxSuppression::Result suppressResult = m_retxSuppression.decide(inFace, interest, *pitEntry);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070060 switch (suppressResult) {
Junxiao Shia788e252015-01-28 17:06:25 -070061 case RetxSuppression::NEW:
Junxiao Shi8d843142016-07-11 22:42:42 +000062 this->afterReceiveNewInterest(inFace, interest, pitEntry);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070063 break;
Junxiao Shia788e252015-01-28 17:06:25 -070064 case RetxSuppression::FORWARD:
Junxiao Shi8d843142016-07-11 22:42:42 +000065 this->afterReceiveRetxInterest(inFace, interest, pitEntry);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070066 break;
Junxiao Shia788e252015-01-28 17:06:25 -070067 case RetxSuppression::SUPPRESS:
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070068 NFD_LOG_DEBUG(interest << " interestFrom " << inFace.getId() << " retx-suppress");
69 break;
70 default:
71 BOOST_ASSERT(false);
72 break;
73 }
74}
75
76void
Junxiao Shi15e98b02016-08-12 11:21:44 +000077AccessStrategy::afterReceiveNewInterest(const Face& inFace, const Interest& interest,
78 const shared_ptr<pit::Entry>& pitEntry)
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070079{
Junxiao Shi8d843142016-07-11 22:42:42 +000080 const fib::Entry& fibEntry = this->lookupFib(*pitEntry);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070081 Name miName;
Junxiao Shifc021862016-08-25 21:51:18 +000082 MtInfo* mi = nullptr;
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070083 std::tie(miName, mi) = this->findPrefixMeasurements(*pitEntry);
84
85 // has measurements for Interest Name?
86 if (mi != nullptr) {
87 NFD_LOG_DEBUG(interest << " interestFrom " << inFace.getId() <<
88 " new-interest mi=" << miName);
89
90 // send to last working nexthop
Junxiao Shia7f9a292016-11-22 16:31:38 +000091 bool isSentToLastNexthop = this->sendToLastNexthop(inFace, interest, pitEntry, *mi, fibEntry);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070092
93 if (isSentToLastNexthop) {
94 return;
95 }
96 }
97 else {
98 NFD_LOG_DEBUG(interest << " interestFrom " << inFace.getId() <<
99 " new-interest no-mi");
100 }
101
102 // no measurements, or last working nexthop unavailable
103
Junxiao Shi965d3a42015-06-01 06:55:23 -0700104 // multicast to all nexthops except incoming face
Junxiao Shia7f9a292016-11-22 16:31:38 +0000105 int nMulticastSent = this->multicast(inFace, interest, pitEntry, fibEntry);
106
107 if (nMulticastSent < 1) {
108 this->rejectPendingInterest(pitEntry);
109 }
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700110}
111
112void
Junxiao Shi15e98b02016-08-12 11:21:44 +0000113AccessStrategy::afterReceiveRetxInterest(const Face& inFace, const Interest& interest,
114 const shared_ptr<pit::Entry>& pitEntry)
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700115{
Junxiao Shi8d843142016-07-11 22:42:42 +0000116 const fib::Entry& fibEntry = this->lookupFib(*pitEntry);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700117 NFD_LOG_DEBUG(interest << " interestFrom " << inFace.getId() << " retx-forward");
Junxiao Shia7f9a292016-11-22 16:31:38 +0000118 this->multicast(inFace, interest, pitEntry, fibEntry);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700119}
120
121bool
Junxiao Shia7f9a292016-11-22 16:31:38 +0000122AccessStrategy::sendToLastNexthop(const Face& inFace, const Interest& interest,
123 const shared_ptr<pit::Entry>& pitEntry, MtInfo& mi,
124 const fib::Entry& fibEntry)
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700125{
Junxiao Shicde37ad2015-12-24 01:02:05 -0700126 if (mi.lastNexthop == face::INVALID_FACEID) {
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700127 NFD_LOG_DEBUG(pitEntry->getInterest() << " no-last-nexthop");
128 return false;
129 }
130
131 if (mi.lastNexthop == inFace.getId()) {
132 NFD_LOG_DEBUG(pitEntry->getInterest() << " last-nexthop-is-downstream");
133 return false;
134 }
135
Junxiao Shia7f9a292016-11-22 16:31:38 +0000136 Face* outFace = this->getFace(mi.lastNexthop);
137 if (outFace == nullptr || !fibEntry.hasNextHop(*outFace)) {
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700138 NFD_LOG_DEBUG(pitEntry->getInterest() << " last-nexthop-gone");
139 return false;
140 }
141
Junxiao Shia7f9a292016-11-22 16:31:38 +0000142 if (wouldViolateScope(inFace, interest, *outFace)) {
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700143 NFD_LOG_DEBUG(pitEntry->getInterest() << " last-nexthop-violates-scope");
144 return false;
145 }
146
147 RttEstimator::Duration rto = mi.rtt.computeRto();
148 NFD_LOG_DEBUG(pitEntry->getInterest() << " interestTo " << mi.lastNexthop <<
149 " last-nexthop rto=" << time::duration_cast<time::microseconds>(rto).count());
150
Junxiao Shia7f9a292016-11-22 16:31:38 +0000151 this->sendInterest(pitEntry, *outFace, interest);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700152
153 // schedule RTO timeout
Junxiao Shifc021862016-08-25 21:51:18 +0000154 PitInfo* pi = pitEntry->insertStrategyInfo<PitInfo>().first;
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700155 pi->rtoTimer = scheduler::schedule(rto,
156 bind(&AccessStrategy::afterRtoTimeout, this, weak_ptr<pit::Entry>(pitEntry),
Junxiao Shi8d843142016-07-11 22:42:42 +0000157 inFace.getId(), mi.lastNexthop));
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700158
159 return true;
160}
161
162void
Junxiao Shia7f9a292016-11-22 16:31:38 +0000163AccessStrategy::afterRtoTimeout(weak_ptr<pit::Entry> pitWeak, FaceId inFaceId, FaceId firstOutFaceId)
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700164{
165 shared_ptr<pit::Entry> pitEntry = pitWeak.lock();
166 BOOST_ASSERT(pitEntry != nullptr);
Junxiao Shia7f9a292016-11-22 16:31:38 +0000167 // if pitEntry is gone, RTO timer should have been cancelled
168
169 Face* inFace = this->getFace(inFaceId);
170 if (inFace == nullptr) {
171 NFD_LOG_DEBUG(pitEntry->getInterest() << " timeoutFrom " << firstOutFaceId <<
172 " inFace-gone " << inFaceId);
173 return;
174 }
175
176 pit::InRecordCollection::iterator inRecord = pitEntry->getInRecord(*inFace);
177 BOOST_ASSERT(inRecord != pitEntry->in_end());
178 // in-record is erased only if Interest is satisfied, and RTO timer should have been cancelled
179 // note: if this strategy is extended to send Nacks, that would also erase in-record,
180 // and RTO timer should be cancelled in that case as well
181
182 const Interest& interest = inRecord->getInterest();
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700183
Junxiao Shi8d843142016-07-11 22:42:42 +0000184 const fib::Entry& fibEntry = this->lookupFib(*pitEntry);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700185
Junxiao Shia7f9a292016-11-22 16:31:38 +0000186 NFD_LOG_DEBUG(pitEntry->getInterest() << " timeoutFrom " << firstOutFaceId <<
187 " multicast-except " << firstOutFaceId);
188 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 }
Junxiao Shia7f9a292016-11-22 16:31:38 +0000203 NFD_LOG_DEBUG(pitEntry->getInterest() << " interestTo " << outFace.getId() <<
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700204 " multicast");
Junxiao Shia7f9a292016-11-22 16:31:38 +0000205 this->sendInterest(pitEntry, outFace, interest);
206 ++nSent;
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700207 }
Junxiao Shia7f9a292016-11-22 16:31:38 +0000208 return nSent;
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700209}
210
211void
Junxiao Shi15e98b02016-08-12 11:21:44 +0000212AccessStrategy::beforeSatisfyInterest(const shared_ptr<pit::Entry>& pitEntry,
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700213 const Face& inFace, const Data& data)
214{
Junxiao Shifc021862016-08-25 21:51:18 +0000215 PitInfo* pi = pitEntry->getStrategyInfo<PitInfo>();
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700216 if (pi != nullptr) {
217 pi->rtoTimer.cancel();
218 }
219
Junxiao Shi4846f372016-04-05 13:39:30 -0700220 if (!pitEntry->hasInRecords()) { // already satisfied by another upstream
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700221 NFD_LOG_DEBUG(pitEntry->getInterest() << " dataFrom " << inFace.getId() <<
222 " not-fastest");
223 return;
224 }
225
Junxiao Shi4846f372016-04-05 13:39:30 -0700226 pit::OutRecordCollection::iterator outRecord = pitEntry->getOutRecord(inFace);
227 if (outRecord == pitEntry->out_end()) { // no out-record
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700228 NFD_LOG_DEBUG(pitEntry->getInterest() << " dataFrom " << inFace.getId() <<
229 " no-out-record");
230 return;
231 }
232
233 time::steady_clock::Duration rtt = time::steady_clock::now() - outRecord->getLastRenewed();
234 NFD_LOG_DEBUG(pitEntry->getInterest() << " dataFrom " << inFace.getId() <<
235 " rtt=" << time::duration_cast<time::microseconds>(rtt).count());
236 this->updateMeasurements(inFace, data, time::duration_cast<RttEstimator::Duration>(rtt));
237}
238
239void
240AccessStrategy::updateMeasurements(const Face& inFace, const Data& data,
241 const RttEstimator::Duration& rtt)
242{
Junxiao Shif15058a2016-12-11 20:15:05 +0000243 ///\todo move FaceInfoTable out of AccessStrategy instance, to Measurements or somewhere else
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700244 FaceInfo& fi = m_fit[inFace.getId()];
245 fi.rtt.addMeasurement(rtt);
246
Junxiao Shifc021862016-08-25 21:51:18 +0000247 MtInfo* mi = this->addPrefixMeasurements(data);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700248 if (mi->lastNexthop != inFace.getId()) {
249 mi->lastNexthop = inFace.getId();
250 mi->rtt = fi.rtt;
251 }
252 else {
253 mi->rtt.addMeasurement(rtt);
254 }
255}
256
257AccessStrategy::MtInfo::MtInfo()
Junxiao Shicde37ad2015-12-24 01:02:05 -0700258 : lastNexthop(face::INVALID_FACEID)
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700259 , rtt(1, time::milliseconds(1), 0.1)
260{
261}
262
Junxiao Shifc021862016-08-25 21:51:18 +0000263std::tuple<Name, AccessStrategy::MtInfo*>
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700264AccessStrategy::findPrefixMeasurements(const pit::Entry& pitEntry)
265{
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000266 measurements::Entry* me = this->getMeasurements().findLongestPrefixMatch(pitEntry);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700267 if (me == nullptr) {
Junxiao Shi5b3feb62016-08-19 01:51:41 +0000268 return std::make_tuple(Name(), nullptr);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700269 }
270
Junxiao Shifc021862016-08-25 21:51:18 +0000271 MtInfo* mi = me->getStrategyInfo<MtInfo>();
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700272 BOOST_ASSERT(mi != nullptr);
273 // XXX after runtime strategy change, it's possible that me exists but mi doesn't exist;
274 // this case needs another longest prefix match until mi is found
Junxiao Shi5b3feb62016-08-19 01:51:41 +0000275 return std::make_tuple(me->getName(), mi);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700276}
277
Junxiao Shifc021862016-08-25 21:51:18 +0000278AccessStrategy::MtInfo*
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700279AccessStrategy::addPrefixMeasurements(const Data& data)
280{
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000281 measurements::Entry* me = nullptr;
282 if (!data.getName().empty()) {
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700283 me = this->getMeasurements().get(data.getName().getPrefix(-1));
284 }
285 if (me == nullptr) { // parent of Data Name is not in this strategy, or Data Name is empty
286 me = this->getMeasurements().get(data.getName());
287 // Data Name must be in this strategy
288 BOOST_ASSERT(me != nullptr);
289 }
290
Junxiao Shi7b0f4d12015-05-10 21:40:29 -0700291 static const time::nanoseconds ME_LIFETIME = time::seconds(8);
292 this->getMeasurements().extendLifetime(*me, ME_LIFETIME);
293
Junxiao Shifc021862016-08-25 21:51:18 +0000294 return me->insertStrategyInfo<MtInfo>().first;
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700295}
296
297AccessStrategy::FaceInfo::FaceInfo()
298 : rtt(1, time::milliseconds(1), 0.1)
299{
300}
301
302void
Junxiao Shiae04d342016-07-19 13:20:22 +0000303AccessStrategy::removeFaceInfo(const Face& face)
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700304{
Junxiao Shiae04d342016-07-19 13:20:22 +0000305 m_fit.erase(face.getId());
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700306}
307
308} // namespace fw
309} // namespace nfd