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