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