blob: ce22c227fb764fd053b9a9f7f272b2a13edc8c9b [file] [log] [blame]
Alexander Afanasyev33b72772014-01-26 23:22:58 -08001/* -*- 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 Shi8c8d2182014-01-30 22:33:00 -07008#include "core/logger.hpp"
9#include "best-route-strategy.hpp"
Alexander Afanasyev33b72772014-01-26 23:22:58 -080010
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080011namespace nfd {
Alexander Afanasyev33b72772014-01-26 23:22:58 -080012
Junxiao Shi8c8d2182014-01-30 22:33:00 -070013NFD_LOG_INIT("Forwarder");
14
Junxiao Shi88884492014-02-15 15:57:43 -070015const Name Forwarder::s_localhostName("ndn:/localhost");
16
Junxiao Shic041ca32014-02-25 20:01:15 -070017Forwarder::Forwarder()
18 : m_lastFaceId(0)
Haowei Yuan78c84d12014-02-27 15:35:13 -060019 , m_nameTree(1024) // "1024" could be made as one configurable parameter of the forwarder.
HangZhangad4afd12014-03-01 11:03:08 +080020 , m_fib(m_nameTree)
Haowei Yuan78c84d12014-02-27 15:35:13 -060021 , m_pit(m_nameTree)
Alexander Afanasyev33b72772014-01-26 23:22:58 -080022{
Junxiao Shi8c8d2182014-01-30 22:33:00 -070023 m_strategy = make_shared<fw::BestRouteStrategy>(boost::ref(*this));
Alexander Afanasyev33b72772014-01-26 23:22:58 -080024}
25
26void
Junxiao Shi8c8d2182014-01-30 22:33:00 -070027Forwarder::addFace(shared_ptr<Face> face)
Alexander Afanasyev33b72772014-01-26 23:22:58 -080028{
Junxiao Shi8c8d2182014-01-30 22:33:00 -070029 FaceId faceId = ++m_lastFaceId;
30 face->setId(faceId);
31 m_faces[faceId] = face;
32 NFD_LOG_INFO("addFace id=" << faceId);
33
34 face->onReceiveInterest += bind(&Forwarder::onInterest,
35 this, boost::ref(*face), _1);
36 face->onReceiveData += bind(&Forwarder::onData,
37 this, boost::ref(*face), _1);
Alexander Afanasyev33b72772014-01-26 23:22:58 -080038}
39
40void
Junxiao Shi8c8d2182014-01-30 22:33:00 -070041Forwarder::removeFace(shared_ptr<Face> face)
Alexander Afanasyev33b72772014-01-26 23:22:58 -080042{
Junxiao Shi8c8d2182014-01-30 22:33:00 -070043 FaceId faceId = face->getId();
44 m_faces.erase(faceId);
45 face->setId(INVALID_FACEID);
46 NFD_LOG_INFO("removeFace id=" << faceId);
Junxiao Shic041ca32014-02-25 20:01:15 -070047
Junxiao Shi8c8d2182014-01-30 22:33:00 -070048 // XXX This clears all subscriptions, because EventEmitter
49 // does not support only removing Forwarder's subscription
50 face->onReceiveInterest.clear();
51 face->onReceiveData .clear();
Junxiao Shic041ca32014-02-25 20:01:15 -070052
Junxiao Shi8c8d2182014-01-30 22:33:00 -070053 m_fib.removeNextHopFromAllEntries(face);
Alexander Afanasyev33b72772014-01-26 23:22:58 -080054}
55
Steve DiBenedetto26b730f2014-02-02 18:36:16 -070056shared_ptr<Face>
57Forwarder::getFace(FaceId id)
58{
59 std::map<FaceId, shared_ptr<Face> >::iterator i = m_faces.find(id);
60 return (i == m_faces.end()) ? (shared_ptr<Face>()) : (i->second);
61}
62
Alexander Afanasyev33b72772014-01-26 23:22:58 -080063void
Junxiao Shi8c8d2182014-01-30 22:33:00 -070064Forwarder::onInterest(Face& face, const Interest& interest)
Alexander Afanasyev33b72772014-01-26 23:22:58 -080065{
Junxiao Shi8c8d2182014-01-30 22:33:00 -070066 this->onIncomingInterest(face, interest);
67}
68
69void
70Forwarder::onData(Face& face, const Data& data)
71{
72 this->onIncomingData(face, data);
Alexander Afanasyev33b72772014-01-26 23:22:58 -080073}
74
Junxiao Shid3c792f2014-01-30 00:46:13 -070075void
76Forwarder::onIncomingInterest(Face& inFace, const Interest& interest)
77{
78 // receive Interest
Junxiao Shi8c8d2182014-01-30 22:33:00 -070079 NFD_LOG_DEBUG("onIncomingInterest face=" << inFace.getId() << " interest=" << interest.getName());
Junxiao Shi06887ac2014-02-13 20:15:42 -070080 const_cast<Interest&>(interest).setIncomingFaceId(inFace.getId());
Junxiao Shic041ca32014-02-25 20:01:15 -070081
Junxiao Shi88884492014-02-15 15:57:43 -070082 // /localhost scope control
83 bool violatesLocalhost = !inFace.isLocal() &&
84 s_localhostName.isPrefixOf(interest.getName());
85 if (violatesLocalhost) {
86 NFD_LOG_DEBUG("onIncomingInterest face=" << inFace.getId()
87 << " interest=" << interest.getName() << " violates /localhost");
88 return;
89 }
Junxiao Shic041ca32014-02-25 20:01:15 -070090
Junxiao Shid3c792f2014-01-30 00:46:13 -070091 // PIT insert
92 std::pair<shared_ptr<pit::Entry>, bool>
93 pitInsertResult = m_pit.insert(interest);
94 shared_ptr<pit::Entry> pitEntry = pitInsertResult.first;
Junxiao Shic041ca32014-02-25 20:01:15 -070095
Junxiao Shid3c792f2014-01-30 00:46:13 -070096 // detect loop and record Nonce
97 bool isLoop = ! pitEntry->addNonce(interest.getNonce());
98 if (isLoop) {
99 // goto Interest loop pipeline
100 this->onInterestLoop(inFace, interest, pitEntry);
101 return;
102 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700103
Junxiao Shid3c792f2014-01-30 00:46:13 -0700104 // cancel unsatisfy & straggler timer
105 this->cancelUnsatisfyAndStragglerTimer(pitEntry);
Junxiao Shic041ca32014-02-25 20:01:15 -0700106
Junxiao Shid3c792f2014-01-30 00:46:13 -0700107 const pit::InRecordCollection& inRecords = pitEntry->getInRecords();
108 bool isPending = inRecords.begin() == inRecords.end();
Junxiao Shic041ca32014-02-25 20:01:15 -0700109
Junxiao Shid3c792f2014-01-30 00:46:13 -0700110 if (!isPending) {
111 // CS lookup
112 const Data* csMatch = m_cs.find(interest);
113 if (csMatch != 0) {
114 // XXX should we lookup PIT for other Interests that also match csMatch?
115
116 // goto outgoing Data pipeline
117 this->onOutgoingData(*csMatch, inFace);
118 return;
119 }
120 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700121
Junxiao Shid3c792f2014-01-30 00:46:13 -0700122 // insert InRecord
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700123 pitEntry->insertOrUpdateInRecord(inFace.shared_from_this(), interest);
Junxiao Shic041ca32014-02-25 20:01:15 -0700124
Junxiao Shid3c792f2014-01-30 00:46:13 -0700125 // 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 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700131
Junxiao Shid3c792f2014-01-30 00:46:13 -0700132 // FIB lookup
133 shared_ptr<fib::Entry> fibEntry
134 = m_fib.findLongestPrefixMatch(interest.getName());
135 // TODO use Fib::findParent(pitEntry)
Junxiao Shic041ca32014-02-25 20:01:15 -0700136
Junxiao Shid3c792f2014-01-30 00:46:13 -0700137 // dispatch to strategy
Junxiao Shi88884492014-02-15 15:57:43 -0700138 this->dispatchToStrategy(inFace, interest, fibEntry, pitEntry);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700139}
140
141void
142Forwarder::onInterestLoop(Face& inFace, const Interest& interest,
143 shared_ptr<pit::Entry> pitEntry)
144{
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700145 NFD_LOG_DEBUG("onInterestLoop face=" << inFace.getId() << " interest=" << interest.getName());
146
Junxiao Shid3c792f2014-01-30 00:46:13 -0700147 // do nothing, which means Interest is dropped
148}
149
150void
151Forwarder::onOutgoingInterest(shared_ptr<pit::Entry> pitEntry, Face& outFace)
152{
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700153 NFD_LOG_DEBUG("onOutgoingInterest face=" << outFace.getId() << " interest=" << pitEntry->getName());
154
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700155 // /localhost scope control
156 bool violatesLocalhost = !outFace.isLocal() &&
157 s_localhostName.isPrefixOf(pitEntry->getName());
158 if (violatesLocalhost) {
159 NFD_LOG_DEBUG("onOutgoingInterest face=" << outFace.getId()
160 << " interest=" << pitEntry->getName() << " violates /localhost");
161 return;
162 }
163
Junxiao Shid3c792f2014-01-30 00:46:13 -0700164 // pick Interest
165 const Interest& interest = pitEntry->getInterest();
166 // TODO pick the last incoming Interest
Junxiao Shic041ca32014-02-25 20:01:15 -0700167
Junxiao Shid3c792f2014-01-30 00:46:13 -0700168 // insert OutRecord
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700169 pitEntry->insertOrUpdateOutRecord(outFace.shared_from_this(), interest);
Junxiao Shic041ca32014-02-25 20:01:15 -0700170
Junxiao Shid3c792f2014-01-30 00:46:13 -0700171 // set PIT unsatisfy timer
172 this->setUnsatisfyTimer(pitEntry);
Junxiao Shic041ca32014-02-25 20:01:15 -0700173
Junxiao Shid3c792f2014-01-30 00:46:13 -0700174 // send Interest
175 outFace.sendInterest(interest);
176}
177
178void
Junxiao Shi09498f02014-02-26 19:41:08 -0700179Forwarder::onInterestReject(shared_ptr<pit::Entry> pitEntry)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700180{
Junxiao Shi09498f02014-02-26 19:41:08 -0700181 NFD_LOG_DEBUG("onInterestReject interest=" << pitEntry->getName());
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700182
Junxiao Shid3c792f2014-01-30 00:46:13 -0700183 // set PIT straggler timer
184 this->setStragglerTimer(pitEntry);
185}
186
187void
188Forwarder::onInterestUnsatisfied(shared_ptr<pit::Entry> pitEntry)
189{
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700190 NFD_LOG_DEBUG("onInterestUnsatisfied interest=" << pitEntry->getName());
191
Junxiao Shid3c792f2014-01-30 00:46:13 -0700192 // invoke PIT unsatisfied callback
193 // TODO
Junxiao Shic041ca32014-02-25 20:01:15 -0700194
Haowei Yuan78c84d12014-02-27 15:35:13 -0600195 // PIT erase
196 m_pit.erase(pitEntry);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700197}
198
199void
200Forwarder::onIncomingData(Face& inFace, const Data& data)
201{
202 // receive Data
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700203 NFD_LOG_DEBUG("onIncomingData face=" << inFace.getId() << " data=" << data.getName());
Junxiao Shi06887ac2014-02-13 20:15:42 -0700204 const_cast<Data&>(data).setIncomingFaceId(inFace.getId());
Junxiao Shic041ca32014-02-25 20:01:15 -0700205
Junxiao Shi88884492014-02-15 15:57:43 -0700206 // /localhost scope control
207 bool violatesLocalhost = !inFace.isLocal() &&
208 s_localhostName.isPrefixOf(data.getName());
209 if (violatesLocalhost) {
210 NFD_LOG_DEBUG("onIncomingData face=" << inFace.getId()
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700211 << " data=" << data.getName() << " violates /localhost");
Junxiao Shi88884492014-02-15 15:57:43 -0700212 return;
213 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700214
Junxiao Shid3c792f2014-01-30 00:46:13 -0700215 // PIT match
216 shared_ptr<pit::DataMatchResult> pitMatches = m_pit.findAllDataMatches(data);
217 if (pitMatches->begin() == pitMatches->end()) {
218 // goto Data unsolicited pipeline
219 this->onDataUnsolicited(inFace, data);
220 return;
221 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700222
Junxiao Shid3c792f2014-01-30 00:46:13 -0700223 // CS insert
224 m_cs.insert(data);
Junxiao Shic041ca32014-02-25 20:01:15 -0700225
Junxiao Shid3c792f2014-01-30 00:46:13 -0700226 std::set<shared_ptr<Face> > pendingDownstreams;
227 // foreach PitEntry
228 for (pit::DataMatchResult::iterator it = pitMatches->begin();
229 it != pitMatches->end(); ++it) {
230 shared_ptr<pit::Entry> pitEntry = *it;
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700231 NFD_LOG_DEBUG("onIncomingData matching=" << pitEntry->getName());
Junxiao Shic041ca32014-02-25 20:01:15 -0700232
Junxiao Shid3c792f2014-01-30 00:46:13 -0700233 // cancel unsatisfy & straggler timer
234 this->cancelUnsatisfyAndStragglerTimer(pitEntry);
Junxiao Shic041ca32014-02-25 20:01:15 -0700235
Junxiao Shid3c792f2014-01-30 00:46:13 -0700236 // remember pending downstreams
237 const pit::InRecordCollection& inRecords = pitEntry->getInRecords();
238 for (pit::InRecordCollection::const_iterator it = inRecords.begin();
239 it != inRecords.end(); ++it) {
240 if (it->getExpiry() > time::now()) {
241 pendingDownstreams.insert(it->getFace());
242 }
243 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700244
Junxiao Shid3c792f2014-01-30 00:46:13 -0700245 // mark PIT satisfied
246 pitEntry->deleteInRecords();
247 pitEntry->deleteOutRecord(inFace.shared_from_this());
Junxiao Shic041ca32014-02-25 20:01:15 -0700248
Junxiao Shid3c792f2014-01-30 00:46:13 -0700249 // set PIT straggler timer
250 this->setStragglerTimer(pitEntry);
Junxiao Shic041ca32014-02-25 20:01:15 -0700251
Junxiao Shid3c792f2014-01-30 00:46:13 -0700252 // invoke PIT satisfy callback
253 // TODO
254 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700255
Junxiao Shid3c792f2014-01-30 00:46:13 -0700256 // foreach pending downstream
257 for (std::set<shared_ptr<Face> >::iterator it = pendingDownstreams.begin();
258 it != pendingDownstreams.end(); ++it) {
259 // goto outgoing Data pipeline
260 this->onOutgoingData(data, **it);
261 }
262}
263
264void
265Forwarder::onDataUnsolicited(Face& inFace, const Data& data)
266{
267 // accept to cache?
268 bool acceptToCache = false;// TODO decision
269 if (acceptToCache) {
270 // CS insert
271 m_cs.insert(data);
272 }
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700273
274 NFD_LOG_DEBUG("onDataUnsolicited face=" << inFace.getId() << " data=" << data.getName() << " acceptToCache=" << acceptToCache);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700275}
276
277void
278Forwarder::onOutgoingData(const Data& data, Face& outFace)
279{
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700280 NFD_LOG_DEBUG("onOutgoingData face=" << outFace.getId() << " data=" << data.getName());
281
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700282 // /localhost scope control
283 bool violatesLocalhost = !outFace.isLocal() &&
284 s_localhostName.isPrefixOf(data.getName());
285 if (violatesLocalhost) {
286 NFD_LOG_DEBUG("onOutgoingData face=" << outFace.getId()
287 << " data=" << data.getName() << " violates /localhost");
288 return;
289 }
290
Junxiao Shid3c792f2014-01-30 00:46:13 -0700291 // traffic manager
292 // pass through
Junxiao Shic041ca32014-02-25 20:01:15 -0700293
Junxiao Shid3c792f2014-01-30 00:46:13 -0700294 // send Data
295 outFace.sendData(data);
296}
297
298static inline bool
299compare_InRecord_expiry(const pit::InRecord& a, const pit::InRecord& b)
300{
301 return a.getExpiry() < b.getExpiry();
302}
303
304void
305Forwarder::setUnsatisfyTimer(shared_ptr<pit::Entry> pitEntry)
306{
307 const pit::InRecordCollection& inRecords = pitEntry->getInRecords();
308 pit::InRecordCollection::const_iterator lastExpiring =
309 std::max_element(inRecords.begin(), inRecords.end(),
310 &compare_InRecord_expiry);
311
312 time::Point lastExpiry = lastExpiring->getExpiry();
313 time::Duration lastExpiryFromNow = lastExpiry - time::now();
314 if (lastExpiryFromNow <= time::seconds(0)) {
315 // TODO all InRecords are already expired; will this happen?
316 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700317
318 pitEntry->m_unsatisfyTimer = scheduler::schedule(lastExpiryFromNow,
Junxiao Shid3c792f2014-01-30 00:46:13 -0700319 bind(&Forwarder::onInterestUnsatisfied, this, pitEntry));
320}
321
322void
323Forwarder::setStragglerTimer(shared_ptr<pit::Entry> pitEntry)
324{
325 time::Duration stragglerTime = time::milliseconds(100);
Junxiao Shic041ca32014-02-25 20:01:15 -0700326
327 pitEntry->m_stragglerTimer = scheduler::schedule(stragglerTime,
Haowei Yuan78c84d12014-02-27 15:35:13 -0600328 bind(&Pit::erase, &m_pit, pitEntry));
Junxiao Shid3c792f2014-01-30 00:46:13 -0700329}
330
331void
332Forwarder::cancelUnsatisfyAndStragglerTimer(shared_ptr<pit::Entry> pitEntry)
333{
Junxiao Shic041ca32014-02-25 20:01:15 -0700334 scheduler::cancel(pitEntry->m_unsatisfyTimer);
335 scheduler::cancel(pitEntry->m_stragglerTimer);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700336}
337
Junxiao Shi88884492014-02-15 15:57:43 -0700338void
339Forwarder::dispatchToStrategy(const Face& inFace,
340 const Interest& interest,
341 shared_ptr<fib::Entry> fibEntry,
342 shared_ptr<pit::Entry> pitEntry)
343{
344 m_strategy->afterReceiveInterest(inFace, interest, fibEntry, pitEntry);
345 // TODO dispatch according to fibEntry
346}
Junxiao Shid3c792f2014-01-30 00:46:13 -0700347
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800348} // namespace nfd