Alexander Afanasyev | 33b7277 | 2014-01-26 23:22:58 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Junxiao Shi | faf3eb0 | 2015-02-16 10:50:36 -0700 | [diff] [blame] | 3 | * Copyright (c) 2014-2015, Regents of the University of California, |
| 4 | * 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. |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 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/>. |
Junxiao Shi | af6569a | 2014-06-14 00:01:34 -0700 | [diff] [blame] | 24 | */ |
Alexander Afanasyev | 33b7277 | 2014-01-26 23:22:58 -0800 | [diff] [blame] | 25 | |
| 26 | #include "forwarder.hpp" |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 27 | #include "core/logger.hpp" |
Junxiao Shi | af6569a | 2014-06-14 00:01:34 -0700 | [diff] [blame] | 28 | #include "core/random.hpp" |
Junxiao Shi | faf3eb0 | 2015-02-16 10:50:36 -0700 | [diff] [blame] | 29 | #include "strategy.hpp" |
Alexander Afanasyev | 4401615 | 2015-01-08 21:41:48 -0800 | [diff] [blame] | 30 | #include "face/null-face.hpp" |
Yuanzhi Gao | b13fb57 | 2015-05-04 12:56:48 -0700 | [diff] [blame] | 31 | |
| 32 | #include "utils/ndn-ns3-packet-tag.hpp" |
| 33 | |
Junxiao Shi | af6569a | 2014-06-14 00:01:34 -0700 | [diff] [blame] | 34 | #include <boost/random/uniform_int_distribution.hpp> |
Alexander Afanasyev | 33b7277 | 2014-01-26 23:22:58 -0800 | [diff] [blame] | 35 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 36 | namespace nfd { |
Alexander Afanasyev | 33b7277 | 2014-01-26 23:22:58 -0800 | [diff] [blame] | 37 | |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 38 | NFD_LOG_INIT("Forwarder"); |
| 39 | |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 40 | using fw::Strategy; |
| 41 | |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 42 | const Name Forwarder::LOCALHOST_NAME("ndn:/localhost"); |
Junxiao Shi | 8888449 | 2014-02-15 15:57:43 -0700 | [diff] [blame] | 43 | |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 44 | Forwarder::Forwarder() |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 45 | : m_faceTable(*this) |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame] | 46 | , m_fib(m_nameTree) |
Haowei Yuan | 78c84d1 | 2014-02-27 15:35:13 -0600 | [diff] [blame] | 47 | , m_pit(m_nameTree) |
HangZhang | c85a23c | 2014-03-01 15:55:55 +0800 | [diff] [blame] | 48 | , m_measurements(m_nameTree) |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 49 | , m_strategyChoice(m_nameTree, fw::makeDefaultStrategy(*this)) |
Alexander Afanasyev | 4401615 | 2015-01-08 21:41:48 -0800 | [diff] [blame] | 50 | , m_csFace(make_shared<NullFace>(FaceUri("contentstore://"))) |
Alexander Afanasyev | 33b7277 | 2014-01-26 23:22:58 -0800 | [diff] [blame] | 51 | { |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 52 | fw::installStrategies(*this); |
Alexander Afanasyev | 4401615 | 2015-01-08 21:41:48 -0800 | [diff] [blame] | 53 | getFaceTable().addReserved(m_csFace, FACEID_CONTENT_STORE); |
Alexander Afanasyev | 33b7277 | 2014-01-26 23:22:58 -0800 | [diff] [blame] | 54 | } |
| 55 | |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 56 | Forwarder::~Forwarder() |
| 57 | { |
| 58 | |
| 59 | } |
| 60 | |
Alexander Afanasyev | 33b7277 | 2014-01-26 23:22:58 -0800 | [diff] [blame] | 61 | void |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 62 | Forwarder::onIncomingInterest(Face& inFace, const Interest& interest) |
| 63 | { |
| 64 | // receive Interest |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 65 | NFD_LOG_DEBUG("onIncomingInterest face=" << inFace.getId() << |
| 66 | " interest=" << interest.getName()); |
Junxiao Shi | 06887ac | 2014-02-13 20:15:42 -0700 | [diff] [blame] | 67 | const_cast<Interest&>(interest).setIncomingFaceId(inFace.getId()); |
Junxiao Shi | 33152f1 | 2014-07-16 19:54:32 -0700 | [diff] [blame] | 68 | ++m_counters.getNInInterests(); |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 69 | |
Junxiao Shi | 8888449 | 2014-02-15 15:57:43 -0700 | [diff] [blame] | 70 | // /localhost scope control |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 71 | bool isViolatingLocalhost = !inFace.isLocal() && |
| 72 | LOCALHOST_NAME.isPrefixOf(interest.getName()); |
| 73 | if (isViolatingLocalhost) { |
| 74 | NFD_LOG_DEBUG("onIncomingInterest face=" << inFace.getId() << |
| 75 | " interest=" << interest.getName() << " violates /localhost"); |
| 76 | // (drop) |
Junxiao Shi | 8888449 | 2014-02-15 15:57:43 -0700 | [diff] [blame] | 77 | return; |
| 78 | } |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 79 | |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 80 | // PIT insert |
Junxiao Shi | 4063184 | 2014-03-01 13:52:37 -0700 | [diff] [blame] | 81 | shared_ptr<pit::Entry> pitEntry = m_pit.insert(interest).first; |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 82 | |
Junxiao Shi | a110f26 | 2014-10-12 12:35:20 -0700 | [diff] [blame] | 83 | // detect duplicate Nonce |
| 84 | int dnw = pitEntry->findNonce(interest.getNonce(), inFace); |
| 85 | bool hasDuplicateNonce = (dnw != pit::DUPLICATE_NONCE_NONE) || |
| 86 | m_deadNonceList.has(interest.getName(), interest.getNonce()); |
| 87 | if (hasDuplicateNonce) { |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 88 | // goto Interest loop pipeline |
| 89 | this->onInterestLoop(inFace, interest, pitEntry); |
| 90 | return; |
| 91 | } |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 92 | |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 93 | // cancel unsatisfy & straggler timer |
| 94 | this->cancelUnsatisfyAndStragglerTimer(pitEntry); |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 95 | |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 96 | // is pending? |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 97 | const pit::InRecordCollection& inRecords = pitEntry->getInRecords(); |
Junxiao Shi | e17349a | 2014-03-25 00:55:38 -0700 | [diff] [blame] | 98 | bool isPending = inRecords.begin() != inRecords.end(); |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 99 | if (!isPending) { |
Spyridon Mastorakis | 9d9c9ae | 2014-12-05 22:43:34 -0800 | [diff] [blame] | 100 | if (m_csFromNdnSim == nullptr) { |
| 101 | m_cs.find(interest, |
| 102 | bind(&Forwarder::onContentStoreHit, this, ref(inFace), pitEntry, _1, _2), |
| 103 | bind(&Forwarder::onContentStoreMiss, this, ref(inFace), pitEntry, _1)); |
| 104 | } |
| 105 | else { |
| 106 | shared_ptr<Data> match = m_csFromNdnSim->Lookup(interest.shared_from_this()); |
| 107 | if (match != nullptr) { |
| 108 | this->onContentStoreHit(inFace, pitEntry, interest, *match); |
| 109 | } |
| 110 | else { |
| 111 | this->onContentStoreMiss(inFace, pitEntry, interest); |
| 112 | } |
| 113 | } |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 114 | } |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 115 | else { |
| 116 | this->onContentStoreMiss(inFace, pitEntry, interest); |
| 117 | } |
| 118 | } |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 119 | |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 120 | void |
| 121 | Forwarder::onContentStoreMiss(const Face& inFace, |
| 122 | shared_ptr<pit::Entry> pitEntry, |
| 123 | const Interest& interest) |
| 124 | { |
| 125 | NFD_LOG_DEBUG("onContentStoreMiss interest=" << interest.getName()); |
| 126 | |
| 127 | shared_ptr<Face> face = const_pointer_cast<Face>(inFace.shared_from_this()); |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 128 | // insert InRecord |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 129 | pitEntry->insertOrUpdateInRecord(face, interest); |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 130 | |
Alexander Afanasyev | a57f8b4 | 2014-07-10 20:11:32 -0700 | [diff] [blame] | 131 | // set PIT unsatisfy timer |
| 132 | this->setUnsatisfyTimer(pitEntry); |
| 133 | |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 134 | // FIB lookup |
Junxiao Shi | 4063184 | 2014-03-01 13:52:37 -0700 | [diff] [blame] | 135 | shared_ptr<fib::Entry> fibEntry = m_fib.findLongestPrefixMatch(*pitEntry); |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 136 | |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 137 | // dispatch to strategy |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 138 | this->dispatchToStrategy(pitEntry, bind(&Strategy::afterReceiveInterest, _1, |
Alexander Afanasyev | f698028 | 2014-05-13 18:28:40 -0700 | [diff] [blame] | 139 | cref(inFace), cref(interest), fibEntry, pitEntry)); |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | void |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 143 | Forwarder::onContentStoreHit(const Face& inFace, |
| 144 | shared_ptr<pit::Entry> pitEntry, |
| 145 | const Interest& interest, |
| 146 | const Data& data) |
| 147 | { |
Vince Lehman | faa5c0c | 2015-08-18 12:52:46 -0500 | [diff] [blame] | 148 | NFD_LOG_DEBUG("onContentStoreHit interest=" << interest.getName()); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 149 | |
Alexander Afanasyev | 4401615 | 2015-01-08 21:41:48 -0800 | [diff] [blame] | 150 | beforeSatisfyInterest(*pitEntry, *m_csFace, data); |
| 151 | this->dispatchToStrategy(pitEntry, bind(&Strategy::beforeSatisfyInterest, _1, |
| 152 | pitEntry, cref(*m_csFace), cref(data))); |
| 153 | |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 154 | const_pointer_cast<Data>(data.shared_from_this())->setIncomingFaceId(FACEID_CONTENT_STORE); |
| 155 | // XXX should we lookup PIT for other Interests that also match csMatch? |
| 156 | |
| 157 | // set PIT straggler timer |
| 158 | this->setStragglerTimer(pitEntry, true, data.getFreshnessPeriod()); |
| 159 | |
| 160 | // goto outgoing Data pipeline |
| 161 | this->onOutgoingData(data, *const_pointer_cast<Face>(inFace.shared_from_this())); |
| 162 | } |
| 163 | |
| 164 | void |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 165 | Forwarder::onInterestLoop(Face& inFace, const Interest& interest, |
| 166 | shared_ptr<pit::Entry> pitEntry) |
| 167 | { |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 168 | NFD_LOG_DEBUG("onInterestLoop face=" << inFace.getId() << |
| 169 | " interest=" << interest.getName()); |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 170 | |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 171 | // (drop) |
| 172 | } |
| 173 | |
| 174 | /** \brief compare two InRecords for picking outgoing Interest |
| 175 | * \return true if b is preferred over a |
| 176 | * |
| 177 | * This function should be passed to std::max_element over InRecordCollection. |
| 178 | * The outgoing Interest picked is the last incoming Interest |
| 179 | * that does not come from outFace. |
| 180 | * If all InRecords come from outFace, it's fine to pick that. This happens when |
| 181 | * there's only one InRecord that comes from outFace. The legit use is for |
| 182 | * vehicular network; otherwise, strategy shouldn't send to the sole inFace. |
| 183 | */ |
| 184 | static inline bool |
| 185 | compare_pickInterest(const pit::InRecord& a, const pit::InRecord& b, const Face* outFace) |
| 186 | { |
| 187 | bool isOutFaceA = a.getFace().get() == outFace; |
| 188 | bool isOutFaceB = b.getFace().get() == outFace; |
| 189 | |
| 190 | if (!isOutFaceA && isOutFaceB) { |
| 191 | return false; |
| 192 | } |
| 193 | if (isOutFaceA && !isOutFaceB) { |
| 194 | return true; |
| 195 | } |
| 196 | |
| 197 | return a.getLastRenewed() > b.getLastRenewed(); |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | void |
Junxiao Shi | d938a6b | 2014-05-11 23:40:29 -0700 | [diff] [blame] | 201 | Forwarder::onOutgoingInterest(shared_ptr<pit::Entry> pitEntry, Face& outFace, |
| 202 | bool wantNewNonce) |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 203 | { |
Junxiao Shi | 223271b | 2014-07-03 22:06:13 -0700 | [diff] [blame] | 204 | if (outFace.getId() == INVALID_FACEID) { |
| 205 | NFD_LOG_WARN("onOutgoingInterest face=invalid interest=" << pitEntry->getName()); |
| 206 | return; |
| 207 | } |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 208 | NFD_LOG_DEBUG("onOutgoingInterest face=" << outFace.getId() << |
| 209 | " interest=" << pitEntry->getName()); |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 210 | |
Junxiao Shi | 57f0f31 | 2014-03-16 11:52:20 -0700 | [diff] [blame] | 211 | // scope control |
| 212 | if (pitEntry->violatesScope(outFace)) { |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 213 | NFD_LOG_DEBUG("onOutgoingInterest face=" << outFace.getId() << |
Junxiao Shi | 57f0f31 | 2014-03-16 11:52:20 -0700 | [diff] [blame] | 214 | " interest=" << pitEntry->getName() << " violates scope"); |
Junxiao Shi | 11bd9c2 | 2014-03-13 20:44:13 -0700 | [diff] [blame] | 215 | return; |
| 216 | } |
| 217 | |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 218 | // pick Interest |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 219 | const pit::InRecordCollection& inRecords = pitEntry->getInRecords(); |
| 220 | pit::InRecordCollection::const_iterator pickedInRecord = std::max_element( |
| 221 | inRecords.begin(), inRecords.end(), bind(&compare_pickInterest, _1, _2, &outFace)); |
| 222 | BOOST_ASSERT(pickedInRecord != inRecords.end()); |
Junxiao Shi | d938a6b | 2014-05-11 23:40:29 -0700 | [diff] [blame] | 223 | shared_ptr<Interest> interest = const_pointer_cast<Interest>( |
| 224 | pickedInRecord->getInterest().shared_from_this()); |
| 225 | |
| 226 | if (wantNewNonce) { |
| 227 | interest = make_shared<Interest>(*interest); |
Junxiao Shi | af6569a | 2014-06-14 00:01:34 -0700 | [diff] [blame] | 228 | static boost::random::uniform_int_distribution<uint32_t> dist; |
| 229 | interest->setNonce(dist(getGlobalRng())); |
Junxiao Shi | d938a6b | 2014-05-11 23:40:29 -0700 | [diff] [blame] | 230 | } |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 231 | |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 232 | // insert OutRecord |
Junxiao Shi | d938a6b | 2014-05-11 23:40:29 -0700 | [diff] [blame] | 233 | pitEntry->insertOrUpdateOutRecord(outFace.shared_from_this(), *interest); |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 234 | |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 235 | // send Interest |
Junxiao Shi | d938a6b | 2014-05-11 23:40:29 -0700 | [diff] [blame] | 236 | outFace.sendInterest(*interest); |
Junxiao Shi | 33152f1 | 2014-07-16 19:54:32 -0700 | [diff] [blame] | 237 | ++m_counters.getNOutInterests(); |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | void |
Junxiao Shi | 09498f0 | 2014-02-26 19:41:08 -0700 | [diff] [blame] | 241 | Forwarder::onInterestReject(shared_ptr<pit::Entry> pitEntry) |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 242 | { |
Junxiao Shi | d938a6b | 2014-05-11 23:40:29 -0700 | [diff] [blame] | 243 | if (pitEntry->hasUnexpiredOutRecords()) { |
| 244 | NFD_LOG_ERROR("onInterestReject interest=" << pitEntry->getName() << |
| 245 | " cannot reject forwarded Interest"); |
| 246 | return; |
| 247 | } |
Junxiao Shi | 09498f0 | 2014-02-26 19:41:08 -0700 | [diff] [blame] | 248 | NFD_LOG_DEBUG("onInterestReject interest=" << pitEntry->getName()); |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 249 | |
Alexander Afanasyev | a57f8b4 | 2014-07-10 20:11:32 -0700 | [diff] [blame] | 250 | // cancel unsatisfy & straggler timer |
| 251 | this->cancelUnsatisfyAndStragglerTimer(pitEntry); |
| 252 | |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 253 | // set PIT straggler timer |
Junxiao Shi | a110f26 | 2014-10-12 12:35:20 -0700 | [diff] [blame] | 254 | this->setStragglerTimer(pitEntry, false); |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | void |
| 258 | Forwarder::onInterestUnsatisfied(shared_ptr<pit::Entry> pitEntry) |
| 259 | { |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 260 | NFD_LOG_DEBUG("onInterestUnsatisfied interest=" << pitEntry->getName()); |
| 261 | |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 262 | // invoke PIT unsatisfied callback |
Alexander Afanasyev | 4401615 | 2015-01-08 21:41:48 -0800 | [diff] [blame] | 263 | beforeExpirePendingInterest(*pitEntry); |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 264 | this->dispatchToStrategy(pitEntry, bind(&Strategy::beforeExpirePendingInterest, _1, |
Alexander Afanasyev | f698028 | 2014-05-13 18:28:40 -0700 | [diff] [blame] | 265 | pitEntry)); |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 266 | |
Junxiao Shi | a110f26 | 2014-10-12 12:35:20 -0700 | [diff] [blame] | 267 | // goto Interest Finalize pipeline |
| 268 | this->onInterestFinalize(pitEntry, false); |
| 269 | } |
| 270 | |
| 271 | void |
| 272 | Forwarder::onInterestFinalize(shared_ptr<pit::Entry> pitEntry, bool isSatisfied, |
| 273 | const time::milliseconds& dataFreshnessPeriod) |
| 274 | { |
| 275 | NFD_LOG_DEBUG("onInterestFinalize interest=" << pitEntry->getName() << |
| 276 | (isSatisfied ? " satisfied" : " unsatisfied")); |
| 277 | |
| 278 | // Dead Nonce List insert if necessary |
| 279 | this->insertDeadNonceList(*pitEntry, isSatisfied, dataFreshnessPeriod, 0); |
| 280 | |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 281 | // PIT delete |
Junxiao Shi | d938a6b | 2014-05-11 23:40:29 -0700 | [diff] [blame] | 282 | this->cancelUnsatisfyAndStragglerTimer(pitEntry); |
Haowei Yuan | 78c84d1 | 2014-02-27 15:35:13 -0600 | [diff] [blame] | 283 | m_pit.erase(pitEntry); |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | void |
| 287 | Forwarder::onIncomingData(Face& inFace, const Data& data) |
| 288 | { |
| 289 | // receive Data |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 290 | NFD_LOG_DEBUG("onIncomingData face=" << inFace.getId() << " data=" << data.getName()); |
Junxiao Shi | 06887ac | 2014-02-13 20:15:42 -0700 | [diff] [blame] | 291 | const_cast<Data&>(data).setIncomingFaceId(inFace.getId()); |
Junxiao Shi | 33152f1 | 2014-07-16 19:54:32 -0700 | [diff] [blame] | 292 | ++m_counters.getNInDatas(); |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 293 | |
Junxiao Shi | 8888449 | 2014-02-15 15:57:43 -0700 | [diff] [blame] | 294 | // /localhost scope control |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 295 | bool isViolatingLocalhost = !inFace.isLocal() && |
| 296 | LOCALHOST_NAME.isPrefixOf(data.getName()); |
| 297 | if (isViolatingLocalhost) { |
| 298 | NFD_LOG_DEBUG("onIncomingData face=" << inFace.getId() << |
| 299 | " data=" << data.getName() << " violates /localhost"); |
| 300 | // (drop) |
Junxiao Shi | 8888449 | 2014-02-15 15:57:43 -0700 | [diff] [blame] | 301 | return; |
| 302 | } |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 303 | |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 304 | // PIT match |
Junxiao Shi | b2bcbcd | 2014-11-08 09:30:28 -0700 | [diff] [blame] | 305 | pit::DataMatchResult pitMatches = m_pit.findAllDataMatches(data); |
| 306 | if (pitMatches.begin() == pitMatches.end()) { |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 307 | // goto Data unsolicited pipeline |
| 308 | this->onDataUnsolicited(inFace, data); |
| 309 | return; |
| 310 | } |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 311 | |
Yuanzhi Gao | b13fb57 | 2015-05-04 12:56:48 -0700 | [diff] [blame] | 312 | // Remove Ptr<Packet> from the Data before inserting into cache, serving two purposes |
| 313 | // - reduce amount of memory used by cached entries |
| 314 | // - remove all tags that (e.g., hop count tag) that could have been associated with Ptr<Packet> |
| 315 | // |
| 316 | // Copying of Data is relatively cheap operation, as it copies (mostly) a collection of Blocks |
| 317 | // pointing to the same underlying memory buffer. |
| 318 | shared_ptr<Data> dataCopyWithoutPacket = make_shared<Data>(data); |
| 319 | dataCopyWithoutPacket->removeTag<ns3::ndn::Ns3PacketTag>(); |
| 320 | |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 321 | // CS insert |
Spyridon Mastorakis | 9d9c9ae | 2014-12-05 22:43:34 -0800 | [diff] [blame] | 322 | if (m_csFromNdnSim == nullptr) |
Yuanzhi Gao | b13fb57 | 2015-05-04 12:56:48 -0700 | [diff] [blame] | 323 | m_cs.insert(*dataCopyWithoutPacket); |
Spyridon Mastorakis | 9d9c9ae | 2014-12-05 22:43:34 -0800 | [diff] [blame] | 324 | else |
Yuanzhi Gao | b13fb57 | 2015-05-04 12:56:48 -0700 | [diff] [blame] | 325 | m_csFromNdnSim->Add(dataCopyWithoutPacket); |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 326 | |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 327 | std::set<shared_ptr<Face> > pendingDownstreams; |
| 328 | // foreach PitEntry |
Junxiao Shi | b2bcbcd | 2014-11-08 09:30:28 -0700 | [diff] [blame] | 329 | for (const shared_ptr<pit::Entry>& pitEntry : pitMatches) { |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 330 | NFD_LOG_DEBUG("onIncomingData matching=" << pitEntry->getName()); |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 331 | |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 332 | // cancel unsatisfy & straggler timer |
| 333 | this->cancelUnsatisfyAndStragglerTimer(pitEntry); |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 334 | |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 335 | // remember pending downstreams |
| 336 | const pit::InRecordCollection& inRecords = pitEntry->getInRecords(); |
| 337 | for (pit::InRecordCollection::const_iterator it = inRecords.begin(); |
| 338 | it != inRecords.end(); ++it) { |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 339 | if (it->getExpiry() > time::steady_clock::now()) { |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 340 | pendingDownstreams.insert(it->getFace()); |
| 341 | } |
| 342 | } |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 343 | |
Junxiao Shi | d938a6b | 2014-05-11 23:40:29 -0700 | [diff] [blame] | 344 | // invoke PIT satisfy callback |
Alexander Afanasyev | 4401615 | 2015-01-08 21:41:48 -0800 | [diff] [blame] | 345 | beforeSatisfyInterest(*pitEntry, inFace, data); |
Junxiao Shi | 82e7f58 | 2014-09-07 15:15:40 -0700 | [diff] [blame] | 346 | this->dispatchToStrategy(pitEntry, bind(&Strategy::beforeSatisfyInterest, _1, |
Junxiao Shi | d938a6b | 2014-05-11 23:40:29 -0700 | [diff] [blame] | 347 | pitEntry, cref(inFace), cref(data))); |
| 348 | |
Junxiao Shi | a110f26 | 2014-10-12 12:35:20 -0700 | [diff] [blame] | 349 | // Dead Nonce List insert if necessary (for OutRecord of inFace) |
| 350 | this->insertDeadNonceList(*pitEntry, true, data.getFreshnessPeriod(), &inFace); |
| 351 | |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 352 | // mark PIT satisfied |
| 353 | pitEntry->deleteInRecords(); |
Junxiao Shi | b2bcbcd | 2014-11-08 09:30:28 -0700 | [diff] [blame] | 354 | pitEntry->deleteOutRecord(inFace); |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 355 | |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 356 | // set PIT straggler timer |
Junxiao Shi | a110f26 | 2014-10-12 12:35:20 -0700 | [diff] [blame] | 357 | this->setStragglerTimer(pitEntry, true, data.getFreshnessPeriod()); |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 358 | } |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 359 | |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 360 | // foreach pending downstream |
| 361 | for (std::set<shared_ptr<Face> >::iterator it = pendingDownstreams.begin(); |
| 362 | it != pendingDownstreams.end(); ++it) { |
Junxiao Shi | da006f5 | 2014-05-16 11:18:00 -0700 | [diff] [blame] | 363 | shared_ptr<Face> pendingDownstream = *it; |
| 364 | if (pendingDownstream.get() == &inFace) { |
| 365 | continue; |
| 366 | } |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 367 | // goto outgoing Data pipeline |
Junxiao Shi | da006f5 | 2014-05-16 11:18:00 -0700 | [diff] [blame] | 368 | this->onOutgoingData(data, *pendingDownstream); |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 369 | } |
| 370 | } |
| 371 | |
| 372 | void |
| 373 | Forwarder::onDataUnsolicited(Face& inFace, const Data& data) |
| 374 | { |
| 375 | // accept to cache? |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 376 | bool acceptToCache = inFace.isLocal(); |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 377 | if (acceptToCache) { |
| 378 | // CS insert |
Spyridon Mastorakis | 9d9c9ae | 2014-12-05 22:43:34 -0800 | [diff] [blame] | 379 | if (m_csFromNdnSim == nullptr) |
| 380 | m_cs.insert(data, true); |
| 381 | else |
| 382 | m_csFromNdnSim->Add(data.shared_from_this()); |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 383 | } |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 384 | |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 385 | NFD_LOG_DEBUG("onDataUnsolicited face=" << inFace.getId() << |
| 386 | " data=" << data.getName() << |
| 387 | (acceptToCache ? " cached" : " not cached")); |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | void |
| 391 | Forwarder::onOutgoingData(const Data& data, Face& outFace) |
| 392 | { |
Junxiao Shi | 223271b | 2014-07-03 22:06:13 -0700 | [diff] [blame] | 393 | if (outFace.getId() == INVALID_FACEID) { |
| 394 | NFD_LOG_WARN("onOutgoingData face=invalid data=" << data.getName()); |
| 395 | return; |
| 396 | } |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 397 | NFD_LOG_DEBUG("onOutgoingData face=" << outFace.getId() << " data=" << data.getName()); |
| 398 | |
Junxiao Shi | 9b27bd2 | 2014-02-26 20:29:58 -0700 | [diff] [blame] | 399 | // /localhost scope control |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 400 | bool isViolatingLocalhost = !outFace.isLocal() && |
| 401 | LOCALHOST_NAME.isPrefixOf(data.getName()); |
| 402 | if (isViolatingLocalhost) { |
| 403 | NFD_LOG_DEBUG("onOutgoingData face=" << outFace.getId() << |
| 404 | " data=" << data.getName() << " violates /localhost"); |
| 405 | // (drop) |
Junxiao Shi | 9b27bd2 | 2014-02-26 20:29:58 -0700 | [diff] [blame] | 406 | return; |
| 407 | } |
| 408 | |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 409 | // TODO traffic manager |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 410 | |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 411 | // send Data |
| 412 | outFace.sendData(data); |
Junxiao Shi | 33152f1 | 2014-07-16 19:54:32 -0700 | [diff] [blame] | 413 | ++m_counters.getNOutDatas(); |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | static inline bool |
| 417 | compare_InRecord_expiry(const pit::InRecord& a, const pit::InRecord& b) |
| 418 | { |
| 419 | return a.getExpiry() < b.getExpiry(); |
| 420 | } |
| 421 | |
| 422 | void |
| 423 | Forwarder::setUnsatisfyTimer(shared_ptr<pit::Entry> pitEntry) |
| 424 | { |
| 425 | const pit::InRecordCollection& inRecords = pitEntry->getInRecords(); |
| 426 | pit::InRecordCollection::const_iterator lastExpiring = |
| 427 | std::max_element(inRecords.begin(), inRecords.end(), |
| 428 | &compare_InRecord_expiry); |
| 429 | |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 430 | time::steady_clock::TimePoint lastExpiry = lastExpiring->getExpiry(); |
| 431 | time::nanoseconds lastExpiryFromNow = lastExpiry - time::steady_clock::now(); |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 432 | if (lastExpiryFromNow <= time::seconds(0)) { |
| 433 | // TODO all InRecords are already expired; will this happen? |
| 434 | } |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 435 | |
Junxiao Shi | 9f7455b | 2014-04-07 21:02:16 -0700 | [diff] [blame] | 436 | scheduler::cancel(pitEntry->m_unsatisfyTimer); |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 437 | pitEntry->m_unsatisfyTimer = scheduler::schedule(lastExpiryFromNow, |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 438 | bind(&Forwarder::onInterestUnsatisfied, this, pitEntry)); |
| 439 | } |
| 440 | |
| 441 | void |
Junxiao Shi | a110f26 | 2014-10-12 12:35:20 -0700 | [diff] [blame] | 442 | Forwarder::setStragglerTimer(shared_ptr<pit::Entry> pitEntry, bool isSatisfied, |
| 443 | const time::milliseconds& dataFreshnessPeriod) |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 444 | { |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 445 | time::nanoseconds stragglerTime = time::milliseconds(100); |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 446 | |
Junxiao Shi | 9f7455b | 2014-04-07 21:02:16 -0700 | [diff] [blame] | 447 | scheduler::cancel(pitEntry->m_stragglerTimer); |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 448 | pitEntry->m_stragglerTimer = scheduler::schedule(stragglerTime, |
Junxiao Shi | a110f26 | 2014-10-12 12:35:20 -0700 | [diff] [blame] | 449 | bind(&Forwarder::onInterestFinalize, this, pitEntry, isSatisfied, dataFreshnessPeriod)); |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | void |
| 453 | Forwarder::cancelUnsatisfyAndStragglerTimer(shared_ptr<pit::Entry> pitEntry) |
| 454 | { |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 455 | scheduler::cancel(pitEntry->m_unsatisfyTimer); |
| 456 | scheduler::cancel(pitEntry->m_stragglerTimer); |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 457 | } |
| 458 | |
Junxiao Shi | a110f26 | 2014-10-12 12:35:20 -0700 | [diff] [blame] | 459 | static inline void |
| 460 | insertNonceToDnl(DeadNonceList& dnl, const pit::Entry& pitEntry, |
| 461 | const pit::OutRecord& outRecord) |
| 462 | { |
| 463 | dnl.add(pitEntry.getName(), outRecord.getLastNonce()); |
| 464 | } |
| 465 | |
| 466 | void |
| 467 | Forwarder::insertDeadNonceList(pit::Entry& pitEntry, bool isSatisfied, |
| 468 | const time::milliseconds& dataFreshnessPeriod, |
| 469 | Face* upstream) |
| 470 | { |
| 471 | // need Dead Nonce List insert? |
| 472 | bool needDnl = false; |
| 473 | if (isSatisfied) { |
| 474 | bool hasFreshnessPeriod = dataFreshnessPeriod >= time::milliseconds::zero(); |
| 475 | // Data never becomes stale if it doesn't have FreshnessPeriod field |
| 476 | needDnl = static_cast<bool>(pitEntry.getInterest().getMustBeFresh()) && |
| 477 | (hasFreshnessPeriod && dataFreshnessPeriod < m_deadNonceList.getLifetime()); |
| 478 | } |
| 479 | else { |
| 480 | needDnl = true; |
| 481 | } |
| 482 | |
| 483 | if (!needDnl) { |
| 484 | return; |
| 485 | } |
| 486 | |
| 487 | // Dead Nonce List insert |
| 488 | if (upstream == 0) { |
| 489 | // insert all outgoing Nonces |
| 490 | const pit::OutRecordCollection& outRecords = pitEntry.getOutRecords(); |
| 491 | std::for_each(outRecords.begin(), outRecords.end(), |
| 492 | bind(&insertNonceToDnl, ref(m_deadNonceList), cref(pitEntry), _1)); |
| 493 | } |
| 494 | else { |
| 495 | // insert outgoing Nonce of a specific face |
Junxiao Shi | b2bcbcd | 2014-11-08 09:30:28 -0700 | [diff] [blame] | 496 | pit::OutRecordCollection::const_iterator outRecord = pitEntry.getOutRecord(*upstream); |
Junxiao Shi | a110f26 | 2014-10-12 12:35:20 -0700 | [diff] [blame] | 497 | if (outRecord != pitEntry.getOutRecords().end()) { |
| 498 | m_deadNonceList.add(pitEntry.getName(), outRecord->getLastNonce()); |
| 499 | } |
| 500 | } |
| 501 | } |
| 502 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 503 | } // namespace nfd |