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