blob: df92d1b01f8599fdca0344500dd59616c4654936 [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)
37 : Strategy(forwarder, name)
38 , m_removeFaceInfoConn(this->beforeRemoveFace.connect(
39 bind(&AccessStrategy::removeFaceInfo, this, _1)))
40{
41}
42
Junxiao Shi037f4ab2016-12-13 04:27:06 +000043const Name&
44AccessStrategy::getStrategyName()
45{
46 static Name strategyName("/localhost/nfd/strategy/access/%FD%01");
47 return strategyName;
48}
49
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070050void
Junxiao Shi15e98b02016-08-12 11:21:44 +000051AccessStrategy::afterReceiveInterest(const Face& inFace, const Interest& interest,
52 const shared_ptr<pit::Entry>& pitEntry)
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070053{
Junxiao Shia788e252015-01-28 17:06:25 -070054 RetxSuppression::Result suppressResult = m_retxSuppression.decide(inFace, interest, *pitEntry);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070055 switch (suppressResult) {
Junxiao Shia788e252015-01-28 17:06:25 -070056 case RetxSuppression::NEW:
Junxiao Shi8d843142016-07-11 22:42:42 +000057 this->afterReceiveNewInterest(inFace, interest, pitEntry);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070058 break;
Junxiao Shia788e252015-01-28 17:06:25 -070059 case RetxSuppression::FORWARD:
Junxiao Shi8d843142016-07-11 22:42:42 +000060 this->afterReceiveRetxInterest(inFace, interest, pitEntry);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070061 break;
Junxiao Shia788e252015-01-28 17:06:25 -070062 case RetxSuppression::SUPPRESS:
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070063 NFD_LOG_DEBUG(interest << " interestFrom " << inFace.getId() << " retx-suppress");
64 break;
65 default:
66 BOOST_ASSERT(false);
67 break;
68 }
69}
70
71void
Junxiao Shi15e98b02016-08-12 11:21:44 +000072AccessStrategy::afterReceiveNewInterest(const Face& inFace, const Interest& interest,
73 const shared_ptr<pit::Entry>& pitEntry)
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070074{
Junxiao Shi8d843142016-07-11 22:42:42 +000075 const fib::Entry& fibEntry = this->lookupFib(*pitEntry);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070076 Name miName;
Junxiao Shifc021862016-08-25 21:51:18 +000077 MtInfo* mi = nullptr;
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070078 std::tie(miName, mi) = this->findPrefixMeasurements(*pitEntry);
79
80 // has measurements for Interest Name?
81 if (mi != nullptr) {
82 NFD_LOG_DEBUG(interest << " interestFrom " << inFace.getId() <<
83 " new-interest mi=" << miName);
84
85 // send to last working nexthop
Junxiao Shia7f9a292016-11-22 16:31:38 +000086 bool isSentToLastNexthop = this->sendToLastNexthop(inFace, interest, pitEntry, *mi, fibEntry);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070087
88 if (isSentToLastNexthop) {
89 return;
90 }
91 }
92 else {
93 NFD_LOG_DEBUG(interest << " interestFrom " << inFace.getId() <<
94 " new-interest no-mi");
95 }
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
Junxiao Shia7f9a292016-11-22 16:31:38 +0000100 int nMulticastSent = this->multicast(inFace, interest, pitEntry, fibEntry);
101
102 if (nMulticastSent < 1) {
103 this->rejectPendingInterest(pitEntry);
104 }
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700105}
106
107void
Junxiao Shi15e98b02016-08-12 11:21:44 +0000108AccessStrategy::afterReceiveRetxInterest(const Face& inFace, const Interest& interest,
109 const shared_ptr<pit::Entry>& pitEntry)
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700110{
Junxiao Shi8d843142016-07-11 22:42:42 +0000111 const fib::Entry& fibEntry = this->lookupFib(*pitEntry);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700112 NFD_LOG_DEBUG(interest << " interestFrom " << inFace.getId() << " retx-forward");
Junxiao Shia7f9a292016-11-22 16:31:38 +0000113 this->multicast(inFace, interest, pitEntry, fibEntry);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700114}
115
116bool
Junxiao Shia7f9a292016-11-22 16:31:38 +0000117AccessStrategy::sendToLastNexthop(const Face& inFace, const Interest& interest,
118 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
126 if (mi.lastNexthop == inFace.getId()) {
127 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);
132 if (outFace == nullptr || !fibEntry.hasNextHop(*outFace)) {
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700133 NFD_LOG_DEBUG(pitEntry->getInterest() << " last-nexthop-gone");
134 return false;
135 }
136
Junxiao Shia7f9a292016-11-22 16:31:38 +0000137 if (wouldViolateScope(inFace, interest, *outFace)) {
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700138 NFD_LOG_DEBUG(pitEntry->getInterest() << " last-nexthop-violates-scope");
139 return false;
140 }
141
142 RttEstimator::Duration rto = mi.rtt.computeRto();
143 NFD_LOG_DEBUG(pitEntry->getInterest() << " interestTo " << mi.lastNexthop <<
144 " last-nexthop rto=" << time::duration_cast<time::microseconds>(rto).count());
145
Junxiao Shia7f9a292016-11-22 16:31:38 +0000146 this->sendInterest(pitEntry, *outFace, 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;
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700150 pi->rtoTimer = scheduler::schedule(rto,
151 bind(&AccessStrategy::afterRtoTimeout, this, weak_ptr<pit::Entry>(pitEntry),
Junxiao Shi8d843142016-07-11 22:42:42 +0000152 inFace.getId(), mi.lastNexthop));
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700153
154 return true;
155}
156
157void
Junxiao Shia7f9a292016-11-22 16:31:38 +0000158AccessStrategy::afterRtoTimeout(weak_ptr<pit::Entry> pitWeak, FaceId inFaceId, FaceId firstOutFaceId)
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700159{
160 shared_ptr<pit::Entry> pitEntry = pitWeak.lock();
161 BOOST_ASSERT(pitEntry != nullptr);
Junxiao Shia7f9a292016-11-22 16:31:38 +0000162 // if pitEntry is gone, RTO timer should have been cancelled
163
164 Face* inFace = this->getFace(inFaceId);
165 if (inFace == nullptr) {
166 NFD_LOG_DEBUG(pitEntry->getInterest() << " timeoutFrom " << firstOutFaceId <<
167 " inFace-gone " << inFaceId);
168 return;
169 }
170
171 pit::InRecordCollection::iterator inRecord = pitEntry->getInRecord(*inFace);
172 BOOST_ASSERT(inRecord != pitEntry->in_end());
173 // in-record is erased only if Interest is satisfied, and RTO timer should have been cancelled
174 // note: if this strategy is extended to send Nacks, that would also erase in-record,
175 // and RTO timer should be cancelled in that case as well
176
177 const Interest& interest = inRecord->getInterest();
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700178
Junxiao Shi8d843142016-07-11 22:42:42 +0000179 const fib::Entry& fibEntry = this->lookupFib(*pitEntry);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700180
Junxiao Shia7f9a292016-11-22 16:31:38 +0000181 NFD_LOG_DEBUG(pitEntry->getInterest() << " timeoutFrom " << firstOutFaceId <<
182 " multicast-except " << firstOutFaceId);
183 this->multicast(*inFace, interest, pitEntry, fibEntry, firstOutFaceId);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700184}
185
Junxiao Shia7f9a292016-11-22 16:31:38 +0000186int
187AccessStrategy::multicast(const Face& inFace, const Interest& interest,
188 const shared_ptr<pit::Entry>& pitEntry, const fib::Entry& fibEntry,
189 FaceId exceptFace)
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700190{
Junxiao Shia7f9a292016-11-22 16:31:38 +0000191 int nSent = 0;
Junxiao Shi8d843142016-07-11 22:42:42 +0000192 for (const fib::NextHop& nexthop : fibEntry.getNextHops()) {
Junxiao Shia7f9a292016-11-22 16:31:38 +0000193 Face& outFace = nexthop.getFace();
194 if (&outFace == &inFace || outFace.getId() == exceptFace ||
195 wouldViolateScope(inFace, interest, outFace)) {
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700196 continue;
197 }
Junxiao Shia7f9a292016-11-22 16:31:38 +0000198 NFD_LOG_DEBUG(pitEntry->getInterest() << " interestTo " << outFace.getId() <<
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700199 " multicast");
Junxiao Shia7f9a292016-11-22 16:31:38 +0000200 this->sendInterest(pitEntry, outFace, interest);
201 ++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,
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700208 const Face& inFace, const Data& data)
209{
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
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700216 NFD_LOG_DEBUG(pitEntry->getInterest() << " dataFrom " << inFace.getId() <<
217 " not-fastest");
218 return;
219 }
220
Junxiao Shi4846f372016-04-05 13:39:30 -0700221 pit::OutRecordCollection::iterator outRecord = pitEntry->getOutRecord(inFace);
222 if (outRecord == pitEntry->out_end()) { // no out-record
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700223 NFD_LOG_DEBUG(pitEntry->getInterest() << " dataFrom " << inFace.getId() <<
224 " no-out-record");
225 return;
226 }
227
228 time::steady_clock::Duration rtt = time::steady_clock::now() - outRecord->getLastRenewed();
229 NFD_LOG_DEBUG(pitEntry->getInterest() << " dataFrom " << inFace.getId() <<
230 " rtt=" << time::duration_cast<time::microseconds>(rtt).count());
231 this->updateMeasurements(inFace, data, time::duration_cast<RttEstimator::Duration>(rtt));
232}
233
234void
235AccessStrategy::updateMeasurements(const Face& inFace, const Data& data,
236 const RttEstimator::Duration& rtt)
237{
Junxiao Shif15058a2016-12-11 20:15:05 +0000238 ///\todo move FaceInfoTable out of AccessStrategy instance, to Measurements or somewhere else
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700239 FaceInfo& fi = m_fit[inFace.getId()];
240 fi.rtt.addMeasurement(rtt);
241
Junxiao Shifc021862016-08-25 21:51:18 +0000242 MtInfo* mi = this->addPrefixMeasurements(data);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700243 if (mi->lastNexthop != inFace.getId()) {
244 mi->lastNexthop = inFace.getId();
245 mi->rtt = fi.rtt;
246 }
247 else {
248 mi->rtt.addMeasurement(rtt);
249 }
250}
251
252AccessStrategy::MtInfo::MtInfo()
Junxiao Shicde37ad2015-12-24 01:02:05 -0700253 : lastNexthop(face::INVALID_FACEID)
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700254 , rtt(1, time::milliseconds(1), 0.1)
255{
256}
257
Junxiao Shifc021862016-08-25 21:51:18 +0000258std::tuple<Name, AccessStrategy::MtInfo*>
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700259AccessStrategy::findPrefixMeasurements(const pit::Entry& pitEntry)
260{
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000261 measurements::Entry* me = this->getMeasurements().findLongestPrefixMatch(pitEntry);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700262 if (me == nullptr) {
Junxiao Shi5b3feb62016-08-19 01:51:41 +0000263 return std::make_tuple(Name(), nullptr);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700264 }
265
Junxiao Shifc021862016-08-25 21:51:18 +0000266 MtInfo* mi = me->getStrategyInfo<MtInfo>();
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700267 BOOST_ASSERT(mi != nullptr);
268 // XXX after runtime strategy change, it's possible that me exists but mi doesn't exist;
269 // this case needs another longest prefix match until mi is found
Junxiao Shi5b3feb62016-08-19 01:51:41 +0000270 return std::make_tuple(me->getName(), mi);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700271}
272
Junxiao Shifc021862016-08-25 21:51:18 +0000273AccessStrategy::MtInfo*
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700274AccessStrategy::addPrefixMeasurements(const Data& data)
275{
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000276 measurements::Entry* me = nullptr;
277 if (!data.getName().empty()) {
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700278 me = this->getMeasurements().get(data.getName().getPrefix(-1));
279 }
280 if (me == nullptr) { // parent of Data Name is not in this strategy, or Data Name is empty
281 me = this->getMeasurements().get(data.getName());
282 // Data Name must be in this strategy
283 BOOST_ASSERT(me != nullptr);
284 }
285
Junxiao Shi7b0f4d12015-05-10 21:40:29 -0700286 static const time::nanoseconds ME_LIFETIME = time::seconds(8);
287 this->getMeasurements().extendLifetime(*me, ME_LIFETIME);
288
Junxiao Shifc021862016-08-25 21:51:18 +0000289 return me->insertStrategyInfo<MtInfo>().first;
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700290}
291
292AccessStrategy::FaceInfo::FaceInfo()
293 : rtt(1, time::milliseconds(1), 0.1)
294{
295}
296
297void
Junxiao Shiae04d342016-07-19 13:20:22 +0000298AccessStrategy::removeFaceInfo(const Face& face)
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700299{
Junxiao Shiae04d342016-07-19 13:20:22 +0000300 m_fit.erase(face.getId());
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700301}
302
303} // namespace fw
304} // namespace nfd