Alexander Afanasyev | 33b7277 | 2014-01-26 23:22:58 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (C) 2014 Named Data Networking Project |
| 4 | * See COPYING for copyright and distribution information. |
| 5 | */ |
| 6 | |
| 7 | #include "forwarder.hpp" |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 8 | #include "core/logger.hpp" |
| 9 | #include "best-route-strategy.hpp" |
Alexander Afanasyev | 33b7277 | 2014-01-26 23:22:58 -0800 | [diff] [blame] | 10 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 11 | namespace nfd { |
Alexander Afanasyev | 33b7277 | 2014-01-26 23:22:58 -0800 | [diff] [blame] | 12 | |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 13 | NFD_LOG_INIT("Forwarder"); |
| 14 | |
Junxiao Shi | 8888449 | 2014-02-15 15:57:43 -0700 | [diff] [blame^] | 15 | const Name Forwarder::s_localhostName("ndn:/localhost"); |
| 16 | |
Alexander Afanasyev | 33b7277 | 2014-01-26 23:22:58 -0800 | [diff] [blame] | 17 | Forwarder::Forwarder(boost::asio::io_service& ioService) |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 18 | : m_scheduler(ioService) |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 19 | , m_lastFaceId(0) |
Alexander Afanasyev | 33b7277 | 2014-01-26 23:22:58 -0800 | [diff] [blame] | 20 | { |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 21 | m_strategy = make_shared<fw::BestRouteStrategy>(boost::ref(*this)); |
Alexander Afanasyev | 33b7277 | 2014-01-26 23:22:58 -0800 | [diff] [blame] | 22 | } |
| 23 | |
| 24 | void |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 25 | Forwarder::addFace(shared_ptr<Face> face) |
Alexander Afanasyev | 33b7277 | 2014-01-26 23:22:58 -0800 | [diff] [blame] | 26 | { |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 27 | FaceId faceId = ++m_lastFaceId; |
| 28 | face->setId(faceId); |
| 29 | m_faces[faceId] = face; |
| 30 | NFD_LOG_INFO("addFace id=" << faceId); |
| 31 | |
| 32 | face->onReceiveInterest += bind(&Forwarder::onInterest, |
| 33 | this, boost::ref(*face), _1); |
| 34 | face->onReceiveData += bind(&Forwarder::onData, |
| 35 | this, boost::ref(*face), _1); |
Alexander Afanasyev | 33b7277 | 2014-01-26 23:22:58 -0800 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | void |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 39 | Forwarder::removeFace(shared_ptr<Face> face) |
Alexander Afanasyev | 33b7277 | 2014-01-26 23:22:58 -0800 | [diff] [blame] | 40 | { |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 41 | FaceId faceId = face->getId(); |
| 42 | m_faces.erase(faceId); |
| 43 | face->setId(INVALID_FACEID); |
| 44 | NFD_LOG_INFO("removeFace id=" << faceId); |
| 45 | |
| 46 | // XXX This clears all subscriptions, because EventEmitter |
| 47 | // does not support only removing Forwarder's subscription |
| 48 | face->onReceiveInterest.clear(); |
| 49 | face->onReceiveData .clear(); |
| 50 | |
| 51 | m_fib.removeNextHopFromAllEntries(face); |
Alexander Afanasyev | 33b7277 | 2014-01-26 23:22:58 -0800 | [diff] [blame] | 52 | } |
| 53 | |
Steve DiBenedetto | 26b730f | 2014-02-02 18:36:16 -0700 | [diff] [blame] | 54 | shared_ptr<Face> |
| 55 | Forwarder::getFace(FaceId id) |
| 56 | { |
| 57 | std::map<FaceId, shared_ptr<Face> >::iterator i = m_faces.find(id); |
| 58 | return (i == m_faces.end()) ? (shared_ptr<Face>()) : (i->second); |
| 59 | } |
| 60 | |
Alexander Afanasyev | 33b7277 | 2014-01-26 23:22:58 -0800 | [diff] [blame] | 61 | void |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 62 | Forwarder::onInterest(Face& face, const Interest& interest) |
Alexander Afanasyev | 33b7277 | 2014-01-26 23:22:58 -0800 | [diff] [blame] | 63 | { |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 64 | this->onIncomingInterest(face, interest); |
| 65 | } |
| 66 | |
| 67 | void |
| 68 | Forwarder::onData(Face& face, const Data& data) |
| 69 | { |
| 70 | this->onIncomingData(face, data); |
Alexander Afanasyev | 33b7277 | 2014-01-26 23:22:58 -0800 | [diff] [blame] | 71 | } |
| 72 | |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 73 | void |
| 74 | Forwarder::onIncomingInterest(Face& inFace, const Interest& interest) |
| 75 | { |
| 76 | // receive Interest |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 77 | NFD_LOG_DEBUG("onIncomingInterest face=" << inFace.getId() << " interest=" << interest.getName()); |
Junxiao Shi | 06887ac | 2014-02-13 20:15:42 -0700 | [diff] [blame] | 78 | const_cast<Interest&>(interest).setIncomingFaceId(inFace.getId()); |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 79 | |
Junxiao Shi | 8888449 | 2014-02-15 15:57:43 -0700 | [diff] [blame^] | 80 | // /localhost scope control |
| 81 | bool violatesLocalhost = !inFace.isLocal() && |
| 82 | s_localhostName.isPrefixOf(interest.getName()); |
| 83 | if (violatesLocalhost) { |
| 84 | NFD_LOG_DEBUG("onIncomingInterest face=" << inFace.getId() |
| 85 | << " interest=" << interest.getName() << " violates /localhost"); |
| 86 | return; |
| 87 | } |
| 88 | |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 89 | // PIT insert |
| 90 | std::pair<shared_ptr<pit::Entry>, bool> |
| 91 | pitInsertResult = m_pit.insert(interest); |
| 92 | shared_ptr<pit::Entry> pitEntry = pitInsertResult.first; |
| 93 | |
| 94 | // detect loop and record Nonce |
| 95 | bool isLoop = ! pitEntry->addNonce(interest.getNonce()); |
| 96 | if (isLoop) { |
| 97 | // goto Interest loop pipeline |
| 98 | this->onInterestLoop(inFace, interest, pitEntry); |
| 99 | return; |
| 100 | } |
| 101 | |
| 102 | // cancel unsatisfy & straggler timer |
| 103 | this->cancelUnsatisfyAndStragglerTimer(pitEntry); |
| 104 | |
| 105 | const pit::InRecordCollection& inRecords = pitEntry->getInRecords(); |
| 106 | bool isPending = inRecords.begin() == inRecords.end(); |
| 107 | |
| 108 | if (!isPending) { |
| 109 | // CS lookup |
| 110 | const Data* csMatch = m_cs.find(interest); |
| 111 | if (csMatch != 0) { |
| 112 | // XXX should we lookup PIT for other Interests that also match csMatch? |
| 113 | |
| 114 | // goto outgoing Data pipeline |
| 115 | this->onOutgoingData(*csMatch, inFace); |
| 116 | return; |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | // insert InRecord |
| 121 | pit::InRecordCollection::iterator inRecordIt = |
| 122 | pitEntry->insertOrUpdateInRecord(inFace.shared_from_this(), interest); |
| 123 | |
| 124 | // app chosen nexthops |
| 125 | bool isAppChosenNexthops = false; // TODO get from local control header |
| 126 | if (isAppChosenNexthops) { |
| 127 | // TODO foreach chosen nexthop: goto outgoing Interest pipeline |
| 128 | return; |
| 129 | } |
| 130 | |
| 131 | // FIB lookup |
| 132 | shared_ptr<fib::Entry> fibEntry |
| 133 | = m_fib.findLongestPrefixMatch(interest.getName()); |
| 134 | // TODO use Fib::findParent(pitEntry) |
| 135 | |
| 136 | // dispatch to strategy |
Junxiao Shi | 8888449 | 2014-02-15 15:57:43 -0700 | [diff] [blame^] | 137 | this->dispatchToStrategy(inFace, 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 | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 144 | NFD_LOG_DEBUG("onInterestLoop face=" << inFace.getId() << " interest=" << interest.getName()); |
| 145 | |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 146 | // do nothing, which means Interest is dropped |
| 147 | } |
| 148 | |
| 149 | void |
| 150 | Forwarder::onOutgoingInterest(shared_ptr<pit::Entry> pitEntry, Face& outFace) |
| 151 | { |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 152 | NFD_LOG_DEBUG("onOutgoingInterest face=" << outFace.getId() << " interest=" << pitEntry->getName()); |
| 153 | |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 154 | // pick Interest |
| 155 | const Interest& interest = pitEntry->getInterest(); |
| 156 | // TODO pick the last incoming Interest |
| 157 | |
| 158 | // insert OutRecord |
| 159 | pit::OutRecordCollection::iterator outRecordIt = |
| 160 | pitEntry->insertOrUpdateOutRecord(outFace.shared_from_this(), interest); |
| 161 | |
| 162 | // set PIT unsatisfy timer |
| 163 | this->setUnsatisfyTimer(pitEntry); |
| 164 | |
| 165 | // send Interest |
| 166 | outFace.sendInterest(interest); |
| 167 | } |
| 168 | |
| 169 | void |
| 170 | Forwarder::onInterestRebuff(shared_ptr<pit::Entry> pitEntry) |
| 171 | { |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 172 | NFD_LOG_DEBUG("onInterestRebuff interest=" << pitEntry->getName()); |
| 173 | |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 174 | // set PIT straggler timer |
| 175 | this->setStragglerTimer(pitEntry); |
| 176 | } |
| 177 | |
| 178 | void |
| 179 | Forwarder::onInterestUnsatisfied(shared_ptr<pit::Entry> pitEntry) |
| 180 | { |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 181 | NFD_LOG_DEBUG("onInterestUnsatisfied interest=" << pitEntry->getName()); |
| 182 | |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 183 | // invoke PIT unsatisfied callback |
| 184 | // TODO |
| 185 | |
| 186 | // PIT delete |
| 187 | m_pit.remove(pitEntry); |
| 188 | } |
| 189 | |
| 190 | void |
| 191 | Forwarder::onIncomingData(Face& inFace, const Data& data) |
| 192 | { |
| 193 | // receive Data |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 194 | NFD_LOG_DEBUG("onIncomingData face=" << inFace.getId() << " data=" << data.getName()); |
Junxiao Shi | 06887ac | 2014-02-13 20:15:42 -0700 | [diff] [blame] | 195 | const_cast<Data&>(data).setIncomingFaceId(inFace.getId()); |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 196 | |
Junxiao Shi | 8888449 | 2014-02-15 15:57:43 -0700 | [diff] [blame^] | 197 | // /localhost scope control |
| 198 | bool violatesLocalhost = !inFace.isLocal() && |
| 199 | s_localhostName.isPrefixOf(data.getName()); |
| 200 | if (violatesLocalhost) { |
| 201 | NFD_LOG_DEBUG("onIncomingData face=" << inFace.getId() |
| 202 | << " interest=" << data.getName() << " violates /localhost"); |
| 203 | return; |
| 204 | } |
| 205 | |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 206 | // PIT match |
| 207 | shared_ptr<pit::DataMatchResult> pitMatches = m_pit.findAllDataMatches(data); |
| 208 | if (pitMatches->begin() == pitMatches->end()) { |
| 209 | // goto Data unsolicited pipeline |
| 210 | this->onDataUnsolicited(inFace, data); |
| 211 | return; |
| 212 | } |
| 213 | |
| 214 | // CS insert |
| 215 | m_cs.insert(data); |
| 216 | |
| 217 | std::set<shared_ptr<Face> > pendingDownstreams; |
| 218 | // foreach PitEntry |
| 219 | for (pit::DataMatchResult::iterator it = pitMatches->begin(); |
| 220 | it != pitMatches->end(); ++it) { |
| 221 | shared_ptr<pit::Entry> pitEntry = *it; |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 222 | NFD_LOG_DEBUG("onIncomingData matching=" << pitEntry->getName()); |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 223 | |
| 224 | // cancel unsatisfy & straggler timer |
| 225 | this->cancelUnsatisfyAndStragglerTimer(pitEntry); |
| 226 | |
| 227 | // remember pending downstreams |
| 228 | const pit::InRecordCollection& inRecords = pitEntry->getInRecords(); |
| 229 | for (pit::InRecordCollection::const_iterator it = inRecords.begin(); |
| 230 | it != inRecords.end(); ++it) { |
| 231 | if (it->getExpiry() > time::now()) { |
| 232 | pendingDownstreams.insert(it->getFace()); |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | // mark PIT satisfied |
| 237 | pitEntry->deleteInRecords(); |
| 238 | pitEntry->deleteOutRecord(inFace.shared_from_this()); |
| 239 | |
| 240 | // set PIT straggler timer |
| 241 | this->setStragglerTimer(pitEntry); |
| 242 | |
| 243 | // invoke PIT satisfy callback |
| 244 | // TODO |
| 245 | } |
| 246 | |
| 247 | // foreach pending downstream |
| 248 | for (std::set<shared_ptr<Face> >::iterator it = pendingDownstreams.begin(); |
| 249 | it != pendingDownstreams.end(); ++it) { |
| 250 | // goto outgoing Data pipeline |
| 251 | this->onOutgoingData(data, **it); |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | void |
| 256 | Forwarder::onDataUnsolicited(Face& inFace, const Data& data) |
| 257 | { |
| 258 | // accept to cache? |
| 259 | bool acceptToCache = false;// TODO decision |
| 260 | if (acceptToCache) { |
| 261 | // CS insert |
| 262 | m_cs.insert(data); |
| 263 | } |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 264 | |
| 265 | NFD_LOG_DEBUG("onDataUnsolicited face=" << inFace.getId() << " data=" << data.getName() << " acceptToCache=" << acceptToCache); |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | void |
| 269 | Forwarder::onOutgoingData(const Data& data, Face& outFace) |
| 270 | { |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 271 | NFD_LOG_DEBUG("onOutgoingData face=" << outFace.getId() << " data=" << data.getName()); |
| 272 | |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 273 | // traffic manager |
| 274 | // pass through |
| 275 | |
| 276 | // send Data |
| 277 | outFace.sendData(data); |
| 278 | } |
| 279 | |
| 280 | static inline bool |
| 281 | compare_InRecord_expiry(const pit::InRecord& a, const pit::InRecord& b) |
| 282 | { |
| 283 | return a.getExpiry() < b.getExpiry(); |
| 284 | } |
| 285 | |
| 286 | void |
| 287 | Forwarder::setUnsatisfyTimer(shared_ptr<pit::Entry> pitEntry) |
| 288 | { |
| 289 | const pit::InRecordCollection& inRecords = pitEntry->getInRecords(); |
| 290 | pit::InRecordCollection::const_iterator lastExpiring = |
| 291 | std::max_element(inRecords.begin(), inRecords.end(), |
| 292 | &compare_InRecord_expiry); |
| 293 | |
| 294 | time::Point lastExpiry = lastExpiring->getExpiry(); |
| 295 | time::Duration lastExpiryFromNow = lastExpiry - time::now(); |
| 296 | if (lastExpiryFromNow <= time::seconds(0)) { |
| 297 | // TODO all InRecords are already expired; will this happen? |
| 298 | } |
| 299 | |
| 300 | pitEntry->m_unsatisfyTimer = m_scheduler.scheduleEvent(lastExpiryFromNow, |
| 301 | bind(&Forwarder::onInterestUnsatisfied, this, pitEntry)); |
| 302 | } |
| 303 | |
| 304 | void |
| 305 | Forwarder::setStragglerTimer(shared_ptr<pit::Entry> pitEntry) |
| 306 | { |
| 307 | time::Duration stragglerTime = time::milliseconds(100); |
| 308 | |
| 309 | pitEntry->m_stragglerTimer = m_scheduler.scheduleEvent(stragglerTime, |
| 310 | bind(&Pit::remove, &m_pit, pitEntry)); |
| 311 | } |
| 312 | |
| 313 | void |
| 314 | Forwarder::cancelUnsatisfyAndStragglerTimer(shared_ptr<pit::Entry> pitEntry) |
| 315 | { |
| 316 | m_scheduler.cancelEvent(pitEntry->m_unsatisfyTimer); |
| 317 | m_scheduler.cancelEvent(pitEntry->m_stragglerTimer); |
| 318 | } |
| 319 | |
Junxiao Shi | 8888449 | 2014-02-15 15:57:43 -0700 | [diff] [blame^] | 320 | void |
| 321 | Forwarder::dispatchToStrategy(const Face& inFace, |
| 322 | const Interest& interest, |
| 323 | shared_ptr<fib::Entry> fibEntry, |
| 324 | shared_ptr<pit::Entry> pitEntry) |
| 325 | { |
| 326 | m_strategy->afterReceiveInterest(inFace, interest, fibEntry, pitEntry); |
| 327 | // TODO dispatch according to fibEntry |
| 328 | } |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 329 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 330 | } // namespace nfd |