blob: cc40f24064a66adaf0dc11f7521c36b15cfbc8f6 [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.
20 , m_pit(m_nameTree)
Alexander Afanasyev33b72772014-01-26 23:22:58 -080021{
Junxiao Shi8c8d2182014-01-30 22:33:00 -070022 m_strategy = make_shared<fw::BestRouteStrategy>(boost::ref(*this));
Alexander Afanasyev33b72772014-01-26 23:22:58 -080023}
24
25void
Junxiao Shi8c8d2182014-01-30 22:33:00 -070026Forwarder::addFace(shared_ptr<Face> face)
Alexander Afanasyev33b72772014-01-26 23:22:58 -080027{
Junxiao Shi8c8d2182014-01-30 22:33:00 -070028 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 Afanasyev33b72772014-01-26 23:22:58 -080037}
38
39void
Junxiao Shi8c8d2182014-01-30 22:33:00 -070040Forwarder::removeFace(shared_ptr<Face> face)
Alexander Afanasyev33b72772014-01-26 23:22:58 -080041{
Junxiao Shi8c8d2182014-01-30 22:33:00 -070042 FaceId faceId = face->getId();
43 m_faces.erase(faceId);
44 face->setId(INVALID_FACEID);
45 NFD_LOG_INFO("removeFace id=" << faceId);
Junxiao Shic041ca32014-02-25 20:01:15 -070046
Junxiao Shi8c8d2182014-01-30 22:33:00 -070047 // XXX This clears all subscriptions, because EventEmitter
48 // does not support only removing Forwarder's subscription
49 face->onReceiveInterest.clear();
50 face->onReceiveData .clear();
Junxiao Shic041ca32014-02-25 20:01:15 -070051
Junxiao Shi8c8d2182014-01-30 22:33:00 -070052 m_fib.removeNextHopFromAllEntries(face);
Alexander Afanasyev33b72772014-01-26 23:22:58 -080053}
54
Steve DiBenedetto26b730f2014-02-02 18:36:16 -070055shared_ptr<Face>
56Forwarder::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 Afanasyev33b72772014-01-26 23:22:58 -080062void
Junxiao Shi8c8d2182014-01-30 22:33:00 -070063Forwarder::onInterest(Face& face, const Interest& interest)
Alexander Afanasyev33b72772014-01-26 23:22:58 -080064{
Junxiao Shi8c8d2182014-01-30 22:33:00 -070065 this->onIncomingInterest(face, interest);
66}
67
68void
69Forwarder::onData(Face& face, const Data& data)
70{
71 this->onIncomingData(face, data);
Alexander Afanasyev33b72772014-01-26 23:22:58 -080072}
73
Junxiao Shid3c792f2014-01-30 00:46:13 -070074void
75Forwarder::onIncomingInterest(Face& inFace, const Interest& interest)
76{
77 // receive Interest
Junxiao Shi8c8d2182014-01-30 22:33:00 -070078 NFD_LOG_DEBUG("onIncomingInterest face=" << inFace.getId() << " interest=" << interest.getName());
Junxiao Shi06887ac2014-02-13 20:15:42 -070079 const_cast<Interest&>(interest).setIncomingFaceId(inFace.getId());
Junxiao Shic041ca32014-02-25 20:01:15 -070080
Junxiao Shi88884492014-02-15 15:57:43 -070081 // /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 }
Junxiao Shic041ca32014-02-25 20:01:15 -070089
Junxiao Shid3c792f2014-01-30 00:46:13 -070090 // PIT insert
91 std::pair<shared_ptr<pit::Entry>, bool>
92 pitInsertResult = m_pit.insert(interest);
93 shared_ptr<pit::Entry> pitEntry = pitInsertResult.first;
Junxiao Shic041ca32014-02-25 20:01:15 -070094
Junxiao Shid3c792f2014-01-30 00:46:13 -070095 // 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 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700102
Junxiao Shid3c792f2014-01-30 00:46:13 -0700103 // cancel unsatisfy & straggler timer
104 this->cancelUnsatisfyAndStragglerTimer(pitEntry);
Junxiao Shic041ca32014-02-25 20:01:15 -0700105
Junxiao Shid3c792f2014-01-30 00:46:13 -0700106 const pit::InRecordCollection& inRecords = pitEntry->getInRecords();
107 bool isPending = inRecords.begin() == inRecords.end();
Junxiao Shic041ca32014-02-25 20:01:15 -0700108
Junxiao Shid3c792f2014-01-30 00:46:13 -0700109 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 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700120
Junxiao Shid3c792f2014-01-30 00:46:13 -0700121 // insert InRecord
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700122 pitEntry->insertOrUpdateInRecord(inFace.shared_from_this(), interest);
Junxiao Shic041ca32014-02-25 20:01:15 -0700123
Junxiao Shid3c792f2014-01-30 00:46:13 -0700124 // app chosen nexthops
125 bool isAppChosenNexthops = false; // TODO get from local control header
126 if (isAppChosenNexthops) {
127 // TODO foreach chosen nexthop: goto outgoing Interest pipeline
128 return;
129 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700130
Junxiao Shid3c792f2014-01-30 00:46:13 -0700131 // FIB lookup
132 shared_ptr<fib::Entry> fibEntry
133 = m_fib.findLongestPrefixMatch(interest.getName());
134 // TODO use Fib::findParent(pitEntry)
Junxiao Shic041ca32014-02-25 20:01:15 -0700135
Junxiao Shid3c792f2014-01-30 00:46:13 -0700136 // dispatch to strategy
Junxiao Shi88884492014-02-15 15:57:43 -0700137 this->dispatchToStrategy(inFace, interest, fibEntry, pitEntry);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700138}
139
140void
141Forwarder::onInterestLoop(Face& inFace, const Interest& interest,
142 shared_ptr<pit::Entry> pitEntry)
143{
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700144 NFD_LOG_DEBUG("onInterestLoop face=" << inFace.getId() << " interest=" << interest.getName());
145
Junxiao Shid3c792f2014-01-30 00:46:13 -0700146 // do nothing, which means Interest is dropped
147}
148
149void
150Forwarder::onOutgoingInterest(shared_ptr<pit::Entry> pitEntry, Face& outFace)
151{
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700152 NFD_LOG_DEBUG("onOutgoingInterest face=" << outFace.getId() << " interest=" << pitEntry->getName());
153
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700154 // /localhost scope control
155 bool violatesLocalhost = !outFace.isLocal() &&
156 s_localhostName.isPrefixOf(pitEntry->getName());
157 if (violatesLocalhost) {
158 NFD_LOG_DEBUG("onOutgoingInterest face=" << outFace.getId()
159 << " interest=" << pitEntry->getName() << " violates /localhost");
160 return;
161 }
162
Junxiao Shid3c792f2014-01-30 00:46:13 -0700163 // pick Interest
164 const Interest& interest = pitEntry->getInterest();
165 // TODO pick the last incoming Interest
Junxiao Shic041ca32014-02-25 20:01:15 -0700166
Junxiao Shid3c792f2014-01-30 00:46:13 -0700167 // insert OutRecord
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700168 pitEntry->insertOrUpdateOutRecord(outFace.shared_from_this(), interest);
Junxiao Shic041ca32014-02-25 20:01:15 -0700169
Junxiao Shid3c792f2014-01-30 00:46:13 -0700170 // set PIT unsatisfy timer
171 this->setUnsatisfyTimer(pitEntry);
Junxiao Shic041ca32014-02-25 20:01:15 -0700172
Junxiao Shid3c792f2014-01-30 00:46:13 -0700173 // send Interest
174 outFace.sendInterest(interest);
175}
176
177void
Junxiao Shi09498f02014-02-26 19:41:08 -0700178Forwarder::onInterestReject(shared_ptr<pit::Entry> pitEntry)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700179{
Junxiao Shi09498f02014-02-26 19:41:08 -0700180 NFD_LOG_DEBUG("onInterestReject interest=" << pitEntry->getName());
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700181
Junxiao Shid3c792f2014-01-30 00:46:13 -0700182 // set PIT straggler timer
183 this->setStragglerTimer(pitEntry);
184}
185
186void
187Forwarder::onInterestUnsatisfied(shared_ptr<pit::Entry> pitEntry)
188{
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700189 NFD_LOG_DEBUG("onInterestUnsatisfied interest=" << pitEntry->getName());
190
Junxiao Shid3c792f2014-01-30 00:46:13 -0700191 // invoke PIT unsatisfied callback
192 // TODO
Junxiao Shic041ca32014-02-25 20:01:15 -0700193
Haowei Yuan78c84d12014-02-27 15:35:13 -0600194 // PIT erase
195 m_pit.erase(pitEntry);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700196}
197
198void
199Forwarder::onIncomingData(Face& inFace, const Data& data)
200{
201 // receive Data
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700202 NFD_LOG_DEBUG("onIncomingData face=" << inFace.getId() << " data=" << data.getName());
Junxiao Shi06887ac2014-02-13 20:15:42 -0700203 const_cast<Data&>(data).setIncomingFaceId(inFace.getId());
Junxiao Shic041ca32014-02-25 20:01:15 -0700204
Junxiao Shi88884492014-02-15 15:57:43 -0700205 // /localhost scope control
206 bool violatesLocalhost = !inFace.isLocal() &&
207 s_localhostName.isPrefixOf(data.getName());
208 if (violatesLocalhost) {
209 NFD_LOG_DEBUG("onIncomingData face=" << inFace.getId()
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700210 << " data=" << data.getName() << " violates /localhost");
Junxiao Shi88884492014-02-15 15:57:43 -0700211 return;
212 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700213
Junxiao Shid3c792f2014-01-30 00:46:13 -0700214 // PIT match
215 shared_ptr<pit::DataMatchResult> pitMatches = m_pit.findAllDataMatches(data);
216 if (pitMatches->begin() == pitMatches->end()) {
217 // goto Data unsolicited pipeline
218 this->onDataUnsolicited(inFace, data);
219 return;
220 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700221
Junxiao Shid3c792f2014-01-30 00:46:13 -0700222 // CS insert
223 m_cs.insert(data);
Junxiao Shic041ca32014-02-25 20:01:15 -0700224
Junxiao Shid3c792f2014-01-30 00:46:13 -0700225 std::set<shared_ptr<Face> > pendingDownstreams;
226 // foreach PitEntry
227 for (pit::DataMatchResult::iterator it = pitMatches->begin();
228 it != pitMatches->end(); ++it) {
229 shared_ptr<pit::Entry> pitEntry = *it;
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700230 NFD_LOG_DEBUG("onIncomingData matching=" << pitEntry->getName());
Junxiao Shic041ca32014-02-25 20:01:15 -0700231
Junxiao Shid3c792f2014-01-30 00:46:13 -0700232 // cancel unsatisfy & straggler timer
233 this->cancelUnsatisfyAndStragglerTimer(pitEntry);
Junxiao Shic041ca32014-02-25 20:01:15 -0700234
Junxiao Shid3c792f2014-01-30 00:46:13 -0700235 // remember pending downstreams
236 const pit::InRecordCollection& inRecords = pitEntry->getInRecords();
237 for (pit::InRecordCollection::const_iterator it = inRecords.begin();
238 it != inRecords.end(); ++it) {
239 if (it->getExpiry() > time::now()) {
240 pendingDownstreams.insert(it->getFace());
241 }
242 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700243
Junxiao Shid3c792f2014-01-30 00:46:13 -0700244 // mark PIT satisfied
245 pitEntry->deleteInRecords();
246 pitEntry->deleteOutRecord(inFace.shared_from_this());
Junxiao Shic041ca32014-02-25 20:01:15 -0700247
Junxiao Shid3c792f2014-01-30 00:46:13 -0700248 // set PIT straggler timer
249 this->setStragglerTimer(pitEntry);
Junxiao Shic041ca32014-02-25 20:01:15 -0700250
Junxiao Shid3c792f2014-01-30 00:46:13 -0700251 // invoke PIT satisfy callback
252 // TODO
253 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700254
Junxiao Shid3c792f2014-01-30 00:46:13 -0700255 // foreach pending downstream
256 for (std::set<shared_ptr<Face> >::iterator it = pendingDownstreams.begin();
257 it != pendingDownstreams.end(); ++it) {
258 // goto outgoing Data pipeline
259 this->onOutgoingData(data, **it);
260 }
261}
262
263void
264Forwarder::onDataUnsolicited(Face& inFace, const Data& data)
265{
266 // accept to cache?
267 bool acceptToCache = false;// TODO decision
268 if (acceptToCache) {
269 // CS insert
270 m_cs.insert(data);
271 }
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700272
273 NFD_LOG_DEBUG("onDataUnsolicited face=" << inFace.getId() << " data=" << data.getName() << " acceptToCache=" << acceptToCache);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700274}
275
276void
277Forwarder::onOutgoingData(const Data& data, Face& outFace)
278{
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700279 NFD_LOG_DEBUG("onOutgoingData face=" << outFace.getId() << " data=" << data.getName());
280
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700281 // /localhost scope control
282 bool violatesLocalhost = !outFace.isLocal() &&
283 s_localhostName.isPrefixOf(data.getName());
284 if (violatesLocalhost) {
285 NFD_LOG_DEBUG("onOutgoingData face=" << outFace.getId()
286 << " data=" << data.getName() << " violates /localhost");
287 return;
288 }
289
Junxiao Shid3c792f2014-01-30 00:46:13 -0700290 // traffic manager
291 // pass through
Junxiao Shic041ca32014-02-25 20:01:15 -0700292
Junxiao Shid3c792f2014-01-30 00:46:13 -0700293 // send Data
294 outFace.sendData(data);
295}
296
297static inline bool
298compare_InRecord_expiry(const pit::InRecord& a, const pit::InRecord& b)
299{
300 return a.getExpiry() < b.getExpiry();
301}
302
303void
304Forwarder::setUnsatisfyTimer(shared_ptr<pit::Entry> pitEntry)
305{
306 const pit::InRecordCollection& inRecords = pitEntry->getInRecords();
307 pit::InRecordCollection::const_iterator lastExpiring =
308 std::max_element(inRecords.begin(), inRecords.end(),
309 &compare_InRecord_expiry);
310
311 time::Point lastExpiry = lastExpiring->getExpiry();
312 time::Duration lastExpiryFromNow = lastExpiry - time::now();
313 if (lastExpiryFromNow <= time::seconds(0)) {
314 // TODO all InRecords are already expired; will this happen?
315 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700316
317 pitEntry->m_unsatisfyTimer = scheduler::schedule(lastExpiryFromNow,
Junxiao Shid3c792f2014-01-30 00:46:13 -0700318 bind(&Forwarder::onInterestUnsatisfied, this, pitEntry));
319}
320
321void
322Forwarder::setStragglerTimer(shared_ptr<pit::Entry> pitEntry)
323{
324 time::Duration stragglerTime = time::milliseconds(100);
Junxiao Shic041ca32014-02-25 20:01:15 -0700325
326 pitEntry->m_stragglerTimer = scheduler::schedule(stragglerTime,
Haowei Yuan78c84d12014-02-27 15:35:13 -0600327 bind(&Pit::erase, &m_pit, pitEntry));
Junxiao Shid3c792f2014-01-30 00:46:13 -0700328}
329
330void
331Forwarder::cancelUnsatisfyAndStragglerTimer(shared_ptr<pit::Entry> pitEntry)
332{
Junxiao Shic041ca32014-02-25 20:01:15 -0700333 scheduler::cancel(pitEntry->m_unsatisfyTimer);
334 scheduler::cancel(pitEntry->m_stragglerTimer);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700335}
336
Junxiao Shi88884492014-02-15 15:57:43 -0700337void
338Forwarder::dispatchToStrategy(const Face& inFace,
339 const Interest& interest,
340 shared_ptr<fib::Entry> fibEntry,
341 shared_ptr<pit::Entry> pitEntry)
342{
343 m_strategy->afterReceiveInterest(inFace, interest, fibEntry, pitEntry);
344 // TODO dispatch according to fibEntry
345}
Junxiao Shid3c792f2014-01-30 00:46:13 -0700346
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800347} // namespace nfd