blob: a7eed839fc7ec4a29274de0145dc54e0e338d394 [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 Shifef73e42016-03-29 14:15:05 -070027#include "pit-algorithm.hpp"
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070028#include "core/logger.hpp"
29
30namespace nfd {
31namespace fw {
32
33NFD_LOG_INIT("AccessStrategy");
34
35const Name AccessStrategy::STRATEGY_NAME("ndn:/localhost/nfd/strategy/access/%FD%01");
Junxiao Shifaf3eb02015-02-16 10:50:36 -070036NFD_REGISTER_STRATEGY(AccessStrategy);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070037
38AccessStrategy::AccessStrategy(Forwarder& forwarder, const Name& name)
39 : Strategy(forwarder, name)
40 , m_removeFaceInfoConn(this->beforeRemoveFace.connect(
41 bind(&AccessStrategy::removeFaceInfo, this, _1)))
42{
43}
44
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070045void
Junxiao Shi15e98b02016-08-12 11:21:44 +000046AccessStrategy::afterReceiveInterest(const Face& inFace, const Interest& interest,
47 const shared_ptr<pit::Entry>& pitEntry)
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070048{
Junxiao Shia788e252015-01-28 17:06:25 -070049 RetxSuppression::Result suppressResult = m_retxSuppression.decide(inFace, interest, *pitEntry);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070050 switch (suppressResult) {
Junxiao Shia788e252015-01-28 17:06:25 -070051 case RetxSuppression::NEW:
Junxiao Shi8d843142016-07-11 22:42:42 +000052 this->afterReceiveNewInterest(inFace, interest, pitEntry);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070053 break;
Junxiao Shia788e252015-01-28 17:06:25 -070054 case RetxSuppression::FORWARD:
Junxiao Shi8d843142016-07-11 22:42:42 +000055 this->afterReceiveRetxInterest(inFace, interest, pitEntry);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070056 break;
Junxiao Shia788e252015-01-28 17:06:25 -070057 case RetxSuppression::SUPPRESS:
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070058 NFD_LOG_DEBUG(interest << " interestFrom " << inFace.getId() << " retx-suppress");
59 break;
60 default:
61 BOOST_ASSERT(false);
62 break;
63 }
64}
65
66void
Junxiao Shi15e98b02016-08-12 11:21:44 +000067AccessStrategy::afterReceiveNewInterest(const Face& inFace, const Interest& interest,
68 const shared_ptr<pit::Entry>& pitEntry)
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070069{
Junxiao Shi8d843142016-07-11 22:42:42 +000070 const fib::Entry& fibEntry = this->lookupFib(*pitEntry);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070071 Name miName;
72 shared_ptr<MtInfo> mi;
73 std::tie(miName, mi) = this->findPrefixMeasurements(*pitEntry);
74
75 // has measurements for Interest Name?
76 if (mi != nullptr) {
77 NFD_LOG_DEBUG(interest << " interestFrom " << inFace.getId() <<
78 " new-interest mi=" << miName);
79
80 // send to last working nexthop
81 bool isSentToLastNexthop = this->sendToLastNexthop(inFace, pitEntry, *mi, fibEntry);
82
83 if (isSentToLastNexthop) {
84 return;
85 }
86 }
87 else {
88 NFD_LOG_DEBUG(interest << " interestFrom " << inFace.getId() <<
89 " new-interest no-mi");
90 }
91
92 // no measurements, or last working nexthop unavailable
93
Junxiao Shi965d3a42015-06-01 06:55:23 -070094 // multicast to all nexthops except incoming face
95 this->multicast(pitEntry, fibEntry, {inFace.getId()});
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070096}
97
98void
Junxiao Shi15e98b02016-08-12 11:21:44 +000099AccessStrategy::afterReceiveRetxInterest(const Face& inFace, const Interest& interest,
100 const shared_ptr<pit::Entry>& pitEntry)
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700101{
Junxiao Shi8d843142016-07-11 22:42:42 +0000102 const fib::Entry& fibEntry = this->lookupFib(*pitEntry);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700103 NFD_LOG_DEBUG(interest << " interestFrom " << inFace.getId() << " retx-forward");
Junxiao Shi965d3a42015-06-01 06:55:23 -0700104 this->multicast(pitEntry, fibEntry, {inFace.getId()});
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700105}
106
107bool
Junxiao Shi15e98b02016-08-12 11:21:44 +0000108AccessStrategy::sendToLastNexthop(const Face& inFace, const shared_ptr<pit::Entry>& pitEntry,
109 MtInfo& mi, const fib::Entry& fibEntry)
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700110{
Junxiao Shicde37ad2015-12-24 01:02:05 -0700111 if (mi.lastNexthop == face::INVALID_FACEID) {
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700112 NFD_LOG_DEBUG(pitEntry->getInterest() << " no-last-nexthop");
113 return false;
114 }
115
116 if (mi.lastNexthop == inFace.getId()) {
117 NFD_LOG_DEBUG(pitEntry->getInterest() << " last-nexthop-is-downstream");
118 return false;
119 }
120
Junxiao Shi5b43f9a2016-07-19 13:15:56 +0000121 Face* face = this->getFace(mi.lastNexthop);
Junxiao Shia6de4292016-07-12 02:08:10 +0000122 if (face == nullptr || !fibEntry.hasNextHop(*face)) {
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700123 NFD_LOG_DEBUG(pitEntry->getInterest() << " last-nexthop-gone");
124 return false;
125 }
126
Junxiao Shifef73e42016-03-29 14:15:05 -0700127 if (violatesScope(*pitEntry, *face)) {
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700128 NFD_LOG_DEBUG(pitEntry->getInterest() << " last-nexthop-violates-scope");
129 return false;
130 }
131
132 RttEstimator::Duration rto = mi.rtt.computeRto();
133 NFD_LOG_DEBUG(pitEntry->getInterest() << " interestTo " << mi.lastNexthop <<
134 " last-nexthop rto=" << time::duration_cast<time::microseconds>(rto).count());
135
Junxiao Shia6de4292016-07-12 02:08:10 +0000136 this->sendInterest(pitEntry, *face);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700137
138 // schedule RTO timeout
Junxiao Shi5b3feb62016-08-19 01:51:41 +0000139 shared_ptr<PitInfo> pi = pitEntry->insertStrategyInfo<PitInfo>();
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700140 pi->rtoTimer = scheduler::schedule(rto,
141 bind(&AccessStrategy::afterRtoTimeout, this, weak_ptr<pit::Entry>(pitEntry),
Junxiao Shi8d843142016-07-11 22:42:42 +0000142 inFace.getId(), mi.lastNexthop));
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700143
144 return true;
145}
146
147void
Junxiao Shi15e98b02016-08-12 11:21:44 +0000148AccessStrategy::afterRtoTimeout(weak_ptr<pit::Entry> pitWeak, FaceId inFace, FaceId firstOutFace)
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700149{
150 shared_ptr<pit::Entry> pitEntry = pitWeak.lock();
151 BOOST_ASSERT(pitEntry != nullptr);
152 // pitEntry can't become nullptr, because RTO timer should be cancelled upon pitEntry destruction
153
Junxiao Shi8d843142016-07-11 22:42:42 +0000154 const fib::Entry& fibEntry = this->lookupFib(*pitEntry);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700155
156 NFD_LOG_DEBUG(pitEntry->getInterest() << " timeoutFrom " << firstOutFace <<
157 " multicast-except " << inFace << ',' << firstOutFace);
Junxiao Shi965d3a42015-06-01 06:55:23 -0700158 this->multicast(pitEntry, fibEntry, {inFace, firstOutFace});
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700159}
160
161void
Junxiao Shi15e98b02016-08-12 11:21:44 +0000162AccessStrategy::multicast(const shared_ptr<pit::Entry>& pitEntry, const fib::Entry& fibEntry,
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700163 std::unordered_set<FaceId> exceptFaces)
164{
Junxiao Shi8d843142016-07-11 22:42:42 +0000165 for (const fib::NextHop& nexthop : fibEntry.getNextHops()) {
Junxiao Shia6de4292016-07-12 02:08:10 +0000166 Face& face = nexthop.getFace();
167 if (exceptFaces.count(face.getId()) > 0) {
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700168 continue;
169 }
Junxiao Shia6de4292016-07-12 02:08:10 +0000170 NFD_LOG_DEBUG(pitEntry->getInterest() << " interestTo " << face.getId() <<
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700171 " multicast");
172 this->sendInterest(pitEntry, face);
173 }
174}
175
176void
Junxiao Shi15e98b02016-08-12 11:21:44 +0000177AccessStrategy::beforeSatisfyInterest(const shared_ptr<pit::Entry>& pitEntry,
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700178 const Face& inFace, const Data& data)
179{
180 shared_ptr<PitInfo> pi = pitEntry->getStrategyInfo<PitInfo>();
181 if (pi != nullptr) {
182 pi->rtoTimer.cancel();
183 }
184
Junxiao Shi4846f372016-04-05 13:39:30 -0700185 if (!pitEntry->hasInRecords()) { // already satisfied by another upstream
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700186 NFD_LOG_DEBUG(pitEntry->getInterest() << " dataFrom " << inFace.getId() <<
187 " not-fastest");
188 return;
189 }
190
Junxiao Shi4846f372016-04-05 13:39:30 -0700191 pit::OutRecordCollection::iterator outRecord = pitEntry->getOutRecord(inFace);
192 if (outRecord == pitEntry->out_end()) { // no out-record
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700193 NFD_LOG_DEBUG(pitEntry->getInterest() << " dataFrom " << inFace.getId() <<
194 " no-out-record");
195 return;
196 }
197
198 time::steady_clock::Duration rtt = time::steady_clock::now() - outRecord->getLastRenewed();
199 NFD_LOG_DEBUG(pitEntry->getInterest() << " dataFrom " << inFace.getId() <<
200 " rtt=" << time::duration_cast<time::microseconds>(rtt).count());
201 this->updateMeasurements(inFace, data, time::duration_cast<RttEstimator::Duration>(rtt));
202}
203
204void
205AccessStrategy::updateMeasurements(const Face& inFace, const Data& data,
206 const RttEstimator::Duration& rtt)
207{
208 FaceInfo& fi = m_fit[inFace.getId()];
209 fi.rtt.addMeasurement(rtt);
210
211 shared_ptr<MtInfo> mi = this->addPrefixMeasurements(data);
212 if (mi->lastNexthop != inFace.getId()) {
213 mi->lastNexthop = inFace.getId();
214 mi->rtt = fi.rtt;
215 }
216 else {
217 mi->rtt.addMeasurement(rtt);
218 }
219}
220
221AccessStrategy::MtInfo::MtInfo()
Junxiao Shicde37ad2015-12-24 01:02:05 -0700222 : lastNexthop(face::INVALID_FACEID)
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700223 , rtt(1, time::milliseconds(1), 0.1)
224{
225}
226
227std::tuple<Name, shared_ptr<AccessStrategy::MtInfo>>
228AccessStrategy::findPrefixMeasurements(const pit::Entry& pitEntry)
229{
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000230 measurements::Entry* me = this->getMeasurements().findLongestPrefixMatch(pitEntry);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700231 if (me == nullptr) {
Junxiao Shi5b3feb62016-08-19 01:51:41 +0000232 return std::make_tuple(Name(), nullptr);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700233 }
234
235 shared_ptr<MtInfo> mi = me->getStrategyInfo<MtInfo>();
236 BOOST_ASSERT(mi != nullptr);
237 // XXX after runtime strategy change, it's possible that me exists but mi doesn't exist;
238 // this case needs another longest prefix match until mi is found
Junxiao Shi5b3feb62016-08-19 01:51:41 +0000239 return std::make_tuple(me->getName(), mi);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700240}
241
242shared_ptr<AccessStrategy::MtInfo>
243AccessStrategy::addPrefixMeasurements(const Data& data)
244{
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000245 measurements::Entry* me = nullptr;
246 if (!data.getName().empty()) {
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700247 me = this->getMeasurements().get(data.getName().getPrefix(-1));
248 }
249 if (me == nullptr) { // parent of Data Name is not in this strategy, or Data Name is empty
250 me = this->getMeasurements().get(data.getName());
251 // Data Name must be in this strategy
252 BOOST_ASSERT(me != nullptr);
253 }
254
Junxiao Shi7b0f4d12015-05-10 21:40:29 -0700255 static const time::nanoseconds ME_LIFETIME = time::seconds(8);
256 this->getMeasurements().extendLifetime(*me, ME_LIFETIME);
257
Junxiao Shi5b3feb62016-08-19 01:51:41 +0000258 return me->insertStrategyInfo<MtInfo>();
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700259}
260
261AccessStrategy::FaceInfo::FaceInfo()
262 : rtt(1, time::milliseconds(1), 0.1)
263{
264}
265
266void
Junxiao Shiae04d342016-07-19 13:20:22 +0000267AccessStrategy::removeFaceInfo(const Face& face)
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700268{
Junxiao Shiae04d342016-07-19 13:20:22 +0000269 m_fit.erase(face.getId());
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700270}
271
272} // namespace fw
273} // namespace nfd