blob: 8f6f01590b70e8223c75a0f4d9fceb527e26a45a [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)
Junxiao Shidbe71732014-02-21 22:23:28 -070020 , m_measurements(ioService)
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);
46
47 // XXX This clears all subscriptions, because EventEmitter
48 // does not support only removing Forwarder's subscription
49 face->onReceiveInterest.clear();
50 face->onReceiveData .clear();
51
52 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 Shid3c792f2014-01-30 00:46:13 -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 }
89
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;
94
95 // 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 }
102
103 // cancel unsatisfy & straggler timer
104 this->cancelUnsatisfyAndStragglerTimer(pitEntry);
105
106 const pit::InRecordCollection& inRecords = pitEntry->getInRecords();
107 bool isPending = inRecords.begin() == inRecords.end();
108
109 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 }
120
121 // insert InRecord
122 pit::InRecordCollection::iterator inRecordIt =
123 pitEntry->insertOrUpdateInRecord(inFace.shared_from_this(), interest);
124
125 // 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 }
131
132 // FIB lookup
133 shared_ptr<fib::Entry> fibEntry
134 = m_fib.findLongestPrefixMatch(interest.getName());
135 // TODO use Fib::findParent(pitEntry)
136
137 // 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 Shid3c792f2014-01-30 00:46:13 -0700155 // pick Interest
156 const Interest& interest = pitEntry->getInterest();
157 // TODO pick the last incoming Interest
158
159 // insert OutRecord
160 pit::OutRecordCollection::iterator outRecordIt =
161 pitEntry->insertOrUpdateOutRecord(outFace.shared_from_this(), interest);
162
163 // set PIT unsatisfy timer
164 this->setUnsatisfyTimer(pitEntry);
165
166 // send Interest
167 outFace.sendInterest(interest);
168}
169
170void
171Forwarder::onInterestRebuff(shared_ptr<pit::Entry> pitEntry)
172{
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700173 NFD_LOG_DEBUG("onInterestRebuff interest=" << pitEntry->getName());
174
Junxiao Shid3c792f2014-01-30 00:46:13 -0700175 // set PIT straggler timer
176 this->setStragglerTimer(pitEntry);
177}
178
179void
180Forwarder::onInterestUnsatisfied(shared_ptr<pit::Entry> pitEntry)
181{
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700182 NFD_LOG_DEBUG("onInterestUnsatisfied interest=" << pitEntry->getName());
183
Junxiao Shid3c792f2014-01-30 00:46:13 -0700184 // invoke PIT unsatisfied callback
185 // TODO
186
187 // PIT delete
188 m_pit.remove(pitEntry);
189}
190
191void
192Forwarder::onIncomingData(Face& inFace, const Data& data)
193{
194 // receive Data
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700195 NFD_LOG_DEBUG("onIncomingData face=" << inFace.getId() << " data=" << data.getName());
Junxiao Shi06887ac2014-02-13 20:15:42 -0700196 const_cast<Data&>(data).setIncomingFaceId(inFace.getId());
Junxiao Shid3c792f2014-01-30 00:46:13 -0700197
Junxiao Shi88884492014-02-15 15:57:43 -0700198 // /localhost scope control
199 bool violatesLocalhost = !inFace.isLocal() &&
200 s_localhostName.isPrefixOf(data.getName());
201 if (violatesLocalhost) {
202 NFD_LOG_DEBUG("onIncomingData face=" << inFace.getId()
203 << " interest=" << data.getName() << " violates /localhost");
204 return;
205 }
206
Junxiao Shid3c792f2014-01-30 00:46:13 -0700207 // PIT match
208 shared_ptr<pit::DataMatchResult> pitMatches = m_pit.findAllDataMatches(data);
209 if (pitMatches->begin() == pitMatches->end()) {
210 // goto Data unsolicited pipeline
211 this->onDataUnsolicited(inFace, data);
212 return;
213 }
214
215 // CS insert
216 m_cs.insert(data);
217
218 std::set<shared_ptr<Face> > pendingDownstreams;
219 // foreach PitEntry
220 for (pit::DataMatchResult::iterator it = pitMatches->begin();
221 it != pitMatches->end(); ++it) {
222 shared_ptr<pit::Entry> pitEntry = *it;
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700223 NFD_LOG_DEBUG("onIncomingData matching=" << pitEntry->getName());
Junxiao Shid3c792f2014-01-30 00:46:13 -0700224
225 // cancel unsatisfy & straggler timer
226 this->cancelUnsatisfyAndStragglerTimer(pitEntry);
227
228 // remember pending downstreams
229 const pit::InRecordCollection& inRecords = pitEntry->getInRecords();
230 for (pit::InRecordCollection::const_iterator it = inRecords.begin();
231 it != inRecords.end(); ++it) {
232 if (it->getExpiry() > time::now()) {
233 pendingDownstreams.insert(it->getFace());
234 }
235 }
236
237 // mark PIT satisfied
238 pitEntry->deleteInRecords();
239 pitEntry->deleteOutRecord(inFace.shared_from_this());
240
241 // set PIT straggler timer
242 this->setStragglerTimer(pitEntry);
243
244 // invoke PIT satisfy callback
245 // TODO
246 }
247
248 // foreach pending downstream
249 for (std::set<shared_ptr<Face> >::iterator it = pendingDownstreams.begin();
250 it != pendingDownstreams.end(); ++it) {
251 // goto outgoing Data pipeline
252 this->onOutgoingData(data, **it);
253 }
254}
255
256void
257Forwarder::onDataUnsolicited(Face& inFace, const Data& data)
258{
259 // accept to cache?
260 bool acceptToCache = false;// TODO decision
261 if (acceptToCache) {
262 // CS insert
263 m_cs.insert(data);
264 }
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700265
266 NFD_LOG_DEBUG("onDataUnsolicited face=" << inFace.getId() << " data=" << data.getName() << " acceptToCache=" << acceptToCache);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700267}
268
269void
270Forwarder::onOutgoingData(const Data& data, Face& outFace)
271{
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700272 NFD_LOG_DEBUG("onOutgoingData face=" << outFace.getId() << " data=" << data.getName());
273
Junxiao Shid3c792f2014-01-30 00:46:13 -0700274 // traffic manager
275 // pass through
276
277 // send Data
278 outFace.sendData(data);
279}
280
281static inline bool
282compare_InRecord_expiry(const pit::InRecord& a, const pit::InRecord& b)
283{
284 return a.getExpiry() < b.getExpiry();
285}
286
287void
288Forwarder::setUnsatisfyTimer(shared_ptr<pit::Entry> pitEntry)
289{
290 const pit::InRecordCollection& inRecords = pitEntry->getInRecords();
291 pit::InRecordCollection::const_iterator lastExpiring =
292 std::max_element(inRecords.begin(), inRecords.end(),
293 &compare_InRecord_expiry);
294
295 time::Point lastExpiry = lastExpiring->getExpiry();
296 time::Duration lastExpiryFromNow = lastExpiry - time::now();
297 if (lastExpiryFromNow <= time::seconds(0)) {
298 // TODO all InRecords are already expired; will this happen?
299 }
300
301 pitEntry->m_unsatisfyTimer = m_scheduler.scheduleEvent(lastExpiryFromNow,
302 bind(&Forwarder::onInterestUnsatisfied, this, pitEntry));
303}
304
305void
306Forwarder::setStragglerTimer(shared_ptr<pit::Entry> pitEntry)
307{
308 time::Duration stragglerTime = time::milliseconds(100);
309
310 pitEntry->m_stragglerTimer = m_scheduler.scheduleEvent(stragglerTime,
311 bind(&Pit::remove, &m_pit, pitEntry));
312}
313
314void
315Forwarder::cancelUnsatisfyAndStragglerTimer(shared_ptr<pit::Entry> pitEntry)
316{
317 m_scheduler.cancelEvent(pitEntry->m_unsatisfyTimer);
318 m_scheduler.cancelEvent(pitEntry->m_stragglerTimer);
319}
320
Junxiao Shi88884492014-02-15 15:57:43 -0700321void
322Forwarder::dispatchToStrategy(const Face& inFace,
323 const Interest& interest,
324 shared_ptr<fib::Entry> fibEntry,
325 shared_ptr<pit::Entry> pitEntry)
326{
327 m_strategy->afterReceiveInterest(inFace, interest, fibEntry, pitEntry);
328 // TODO dispatch according to fibEntry
329}
Junxiao Shid3c792f2014-01-30 00:46:13 -0700330
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800331} // namespace nfd