blob: 6ab6fd418ef1d02a9a7204994be4b1a42a394a64 [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
Alexander Afanasyev33b72772014-01-26 23:22:58 -080017Forwarder::Forwarder(boost::asio::io_service& ioService)
Junxiao Shid3c792f2014-01-30 00:46:13 -070018 : m_scheduler(ioService)
Junxiao Shi8c8d2182014-01-30 22:33:00 -070019 , m_lastFaceId(0)
Alexander Afanasyev33b72772014-01-26 23:22:58 -080020{
Junxiao Shi8c8d2182014-01-30 22:33:00 -070021 m_strategy = make_shared<fw::BestRouteStrategy>(boost::ref(*this));
Alexander Afanasyev33b72772014-01-26 23:22:58 -080022}
23
24void
Junxiao Shi8c8d2182014-01-30 22:33:00 -070025Forwarder::addFace(shared_ptr<Face> face)
Alexander Afanasyev33b72772014-01-26 23:22:58 -080026{
Junxiao Shi8c8d2182014-01-30 22:33:00 -070027 FaceId faceId = ++m_lastFaceId;
28 face->setId(faceId);
29 m_faces[faceId] = face;
30 NFD_LOG_INFO("addFace id=" << faceId);
31
32 face->onReceiveInterest += bind(&Forwarder::onInterest,
33 this, boost::ref(*face), _1);
34 face->onReceiveData += bind(&Forwarder::onData,
35 this, boost::ref(*face), _1);
Alexander Afanasyev33b72772014-01-26 23:22:58 -080036}
37
38void
Junxiao Shi8c8d2182014-01-30 22:33:00 -070039Forwarder::removeFace(shared_ptr<Face> face)
Alexander Afanasyev33b72772014-01-26 23:22:58 -080040{
Junxiao Shi8c8d2182014-01-30 22:33:00 -070041 FaceId faceId = face->getId();
42 m_faces.erase(faceId);
43 face->setId(INVALID_FACEID);
44 NFD_LOG_INFO("removeFace id=" << faceId);
45
46 // XXX This clears all subscriptions, because EventEmitter
47 // does not support only removing Forwarder's subscription
48 face->onReceiveInterest.clear();
49 face->onReceiveData .clear();
50
51 m_fib.removeNextHopFromAllEntries(face);
Alexander Afanasyev33b72772014-01-26 23:22:58 -080052}
53
Steve DiBenedetto26b730f2014-02-02 18:36:16 -070054shared_ptr<Face>
55Forwarder::getFace(FaceId id)
56{
57 std::map<FaceId, shared_ptr<Face> >::iterator i = m_faces.find(id);
58 return (i == m_faces.end()) ? (shared_ptr<Face>()) : (i->second);
59}
60
Alexander Afanasyev33b72772014-01-26 23:22:58 -080061void
Junxiao Shi8c8d2182014-01-30 22:33:00 -070062Forwarder::onInterest(Face& face, const Interest& interest)
Alexander Afanasyev33b72772014-01-26 23:22:58 -080063{
Junxiao Shi8c8d2182014-01-30 22:33:00 -070064 this->onIncomingInterest(face, interest);
65}
66
67void
68Forwarder::onData(Face& face, const Data& data)
69{
70 this->onIncomingData(face, data);
Alexander Afanasyev33b72772014-01-26 23:22:58 -080071}
72
Junxiao Shid3c792f2014-01-30 00:46:13 -070073void
74Forwarder::onIncomingInterest(Face& inFace, const Interest& interest)
75{
76 // receive Interest
Junxiao Shi8c8d2182014-01-30 22:33:00 -070077 NFD_LOG_DEBUG("onIncomingInterest face=" << inFace.getId() << " interest=" << interest.getName());
Junxiao Shi06887ac2014-02-13 20:15:42 -070078 const_cast<Interest&>(interest).setIncomingFaceId(inFace.getId());
Junxiao Shid3c792f2014-01-30 00:46:13 -070079
Junxiao Shi88884492014-02-15 15:57:43 -070080 // /localhost scope control
81 bool violatesLocalhost = !inFace.isLocal() &&
82 s_localhostName.isPrefixOf(interest.getName());
83 if (violatesLocalhost) {
84 NFD_LOG_DEBUG("onIncomingInterest face=" << inFace.getId()
85 << " interest=" << interest.getName() << " violates /localhost");
86 return;
87 }
88
Junxiao Shid3c792f2014-01-30 00:46:13 -070089 // PIT insert
90 std::pair<shared_ptr<pit::Entry>, bool>
91 pitInsertResult = m_pit.insert(interest);
92 shared_ptr<pit::Entry> pitEntry = pitInsertResult.first;
93
94 // detect loop and record Nonce
95 bool isLoop = ! pitEntry->addNonce(interest.getNonce());
96 if (isLoop) {
97 // goto Interest loop pipeline
98 this->onInterestLoop(inFace, interest, pitEntry);
99 return;
100 }
101
102 // cancel unsatisfy & straggler timer
103 this->cancelUnsatisfyAndStragglerTimer(pitEntry);
104
105 const pit::InRecordCollection& inRecords = pitEntry->getInRecords();
106 bool isPending = inRecords.begin() == inRecords.end();
107
108 if (!isPending) {
109 // CS lookup
110 const Data* csMatch = m_cs.find(interest);
111 if (csMatch != 0) {
112 // XXX should we lookup PIT for other Interests that also match csMatch?
113
114 // goto outgoing Data pipeline
115 this->onOutgoingData(*csMatch, inFace);
116 return;
117 }
118 }
119
120 // insert InRecord
121 pit::InRecordCollection::iterator inRecordIt =
122 pitEntry->insertOrUpdateInRecord(inFace.shared_from_this(), interest);
123
124 // 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 }
130
131 // FIB lookup
132 shared_ptr<fib::Entry> fibEntry
133 = m_fib.findLongestPrefixMatch(interest.getName());
134 // TODO use Fib::findParent(pitEntry)
135
136 // 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 Shid3c792f2014-01-30 00:46:13 -0700154 // pick Interest
155 const Interest& interest = pitEntry->getInterest();
156 // TODO pick the last incoming Interest
157
158 // insert OutRecord
159 pit::OutRecordCollection::iterator outRecordIt =
160 pitEntry->insertOrUpdateOutRecord(outFace.shared_from_this(), interest);
161
162 // set PIT unsatisfy timer
163 this->setUnsatisfyTimer(pitEntry);
164
165 // send Interest
166 outFace.sendInterest(interest);
167}
168
169void
170Forwarder::onInterestRebuff(shared_ptr<pit::Entry> pitEntry)
171{
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700172 NFD_LOG_DEBUG("onInterestRebuff interest=" << pitEntry->getName());
173
Junxiao Shid3c792f2014-01-30 00:46:13 -0700174 // set PIT straggler timer
175 this->setStragglerTimer(pitEntry);
176}
177
178void
179Forwarder::onInterestUnsatisfied(shared_ptr<pit::Entry> pitEntry)
180{
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700181 NFD_LOG_DEBUG("onInterestUnsatisfied interest=" << pitEntry->getName());
182
Junxiao Shid3c792f2014-01-30 00:46:13 -0700183 // invoke PIT unsatisfied callback
184 // TODO
185
186 // PIT delete
187 m_pit.remove(pitEntry);
188}
189
190void
191Forwarder::onIncomingData(Face& inFace, const Data& data)
192{
193 // receive Data
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700194 NFD_LOG_DEBUG("onIncomingData face=" << inFace.getId() << " data=" << data.getName());
Junxiao Shi06887ac2014-02-13 20:15:42 -0700195 const_cast<Data&>(data).setIncomingFaceId(inFace.getId());
Junxiao Shid3c792f2014-01-30 00:46:13 -0700196
Junxiao Shi88884492014-02-15 15:57:43 -0700197 // /localhost scope control
198 bool violatesLocalhost = !inFace.isLocal() &&
199 s_localhostName.isPrefixOf(data.getName());
200 if (violatesLocalhost) {
201 NFD_LOG_DEBUG("onIncomingData face=" << inFace.getId()
202 << " interest=" << data.getName() << " violates /localhost");
203 return;
204 }
205
Junxiao Shid3c792f2014-01-30 00:46:13 -0700206 // PIT match
207 shared_ptr<pit::DataMatchResult> pitMatches = m_pit.findAllDataMatches(data);
208 if (pitMatches->begin() == pitMatches->end()) {
209 // goto Data unsolicited pipeline
210 this->onDataUnsolicited(inFace, data);
211 return;
212 }
213
214 // CS insert
215 m_cs.insert(data);
216
217 std::set<shared_ptr<Face> > pendingDownstreams;
218 // foreach PitEntry
219 for (pit::DataMatchResult::iterator it = pitMatches->begin();
220 it != pitMatches->end(); ++it) {
221 shared_ptr<pit::Entry> pitEntry = *it;
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700222 NFD_LOG_DEBUG("onIncomingData matching=" << pitEntry->getName());
Junxiao Shid3c792f2014-01-30 00:46:13 -0700223
224 // cancel unsatisfy & straggler timer
225 this->cancelUnsatisfyAndStragglerTimer(pitEntry);
226
227 // remember pending downstreams
228 const pit::InRecordCollection& inRecords = pitEntry->getInRecords();
229 for (pit::InRecordCollection::const_iterator it = inRecords.begin();
230 it != inRecords.end(); ++it) {
231 if (it->getExpiry() > time::now()) {
232 pendingDownstreams.insert(it->getFace());
233 }
234 }
235
236 // mark PIT satisfied
237 pitEntry->deleteInRecords();
238 pitEntry->deleteOutRecord(inFace.shared_from_this());
239
240 // set PIT straggler timer
241 this->setStragglerTimer(pitEntry);
242
243 // invoke PIT satisfy callback
244 // TODO
245 }
246
247 // foreach pending downstream
248 for (std::set<shared_ptr<Face> >::iterator it = pendingDownstreams.begin();
249 it != pendingDownstreams.end(); ++it) {
250 // goto outgoing Data pipeline
251 this->onOutgoingData(data, **it);
252 }
253}
254
255void
256Forwarder::onDataUnsolicited(Face& inFace, const Data& data)
257{
258 // accept to cache?
259 bool acceptToCache = false;// TODO decision
260 if (acceptToCache) {
261 // CS insert
262 m_cs.insert(data);
263 }
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700264
265 NFD_LOG_DEBUG("onDataUnsolicited face=" << inFace.getId() << " data=" << data.getName() << " acceptToCache=" << acceptToCache);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700266}
267
268void
269Forwarder::onOutgoingData(const Data& data, Face& outFace)
270{
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700271 NFD_LOG_DEBUG("onOutgoingData face=" << outFace.getId() << " data=" << data.getName());
272
Junxiao Shid3c792f2014-01-30 00:46:13 -0700273 // traffic manager
274 // pass through
275
276 // send Data
277 outFace.sendData(data);
278}
279
280static inline bool
281compare_InRecord_expiry(const pit::InRecord& a, const pit::InRecord& b)
282{
283 return a.getExpiry() < b.getExpiry();
284}
285
286void
287Forwarder::setUnsatisfyTimer(shared_ptr<pit::Entry> pitEntry)
288{
289 const pit::InRecordCollection& inRecords = pitEntry->getInRecords();
290 pit::InRecordCollection::const_iterator lastExpiring =
291 std::max_element(inRecords.begin(), inRecords.end(),
292 &compare_InRecord_expiry);
293
294 time::Point lastExpiry = lastExpiring->getExpiry();
295 time::Duration lastExpiryFromNow = lastExpiry - time::now();
296 if (lastExpiryFromNow <= time::seconds(0)) {
297 // TODO all InRecords are already expired; will this happen?
298 }
299
300 pitEntry->m_unsatisfyTimer = m_scheduler.scheduleEvent(lastExpiryFromNow,
301 bind(&Forwarder::onInterestUnsatisfied, this, pitEntry));
302}
303
304void
305Forwarder::setStragglerTimer(shared_ptr<pit::Entry> pitEntry)
306{
307 time::Duration stragglerTime = time::milliseconds(100);
308
309 pitEntry->m_stragglerTimer = m_scheduler.scheduleEvent(stragglerTime,
310 bind(&Pit::remove, &m_pit, pitEntry));
311}
312
313void
314Forwarder::cancelUnsatisfyAndStragglerTimer(shared_ptr<pit::Entry> pitEntry)
315{
316 m_scheduler.cancelEvent(pitEntry->m_unsatisfyTimer);
317 m_scheduler.cancelEvent(pitEntry->m_stragglerTimer);
318}
319
Junxiao Shi88884492014-02-15 15:57:43 -0700320void
321Forwarder::dispatchToStrategy(const Face& inFace,
322 const Interest& interest,
323 shared_ptr<fib::Entry> fibEntry,
324 shared_ptr<pit::Entry> pitEntry)
325{
326 m_strategy->afterReceiveInterest(inFace, interest, fibEntry, pitEntry);
327 // TODO dispatch according to fibEntry
328}
Junxiao Shid3c792f2014-01-30 00:46:13 -0700329
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800330} // namespace nfd