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