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