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