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