blob: 67aeba74643baec1df069f653c5d004215e97a49 [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
Alexander Afanasyev33b72772014-01-26 23:22:58 -080015Forwarder::Forwarder(boost::asio::io_service& ioService)
Junxiao Shid3c792f2014-01-30 00:46:13 -070016 : m_scheduler(ioService)
Junxiao Shi8c8d2182014-01-30 22:33:00 -070017 , m_lastFaceId(0)
Alexander Afanasyev33b72772014-01-26 23:22:58 -080018{
Junxiao Shi8c8d2182014-01-30 22:33:00 -070019 m_strategy = make_shared<fw::BestRouteStrategy>(boost::ref(*this));
Alexander Afanasyev33b72772014-01-26 23:22:58 -080020}
21
22void
Junxiao Shi8c8d2182014-01-30 22:33:00 -070023Forwarder::addFace(shared_ptr<Face> face)
Alexander Afanasyev33b72772014-01-26 23:22:58 -080024{
Junxiao Shi8c8d2182014-01-30 22:33:00 -070025 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 Afanasyev33b72772014-01-26 23:22:58 -080034}
35
36void
Junxiao Shi8c8d2182014-01-30 22:33:00 -070037Forwarder::removeFace(shared_ptr<Face> face)
Alexander Afanasyev33b72772014-01-26 23:22:58 -080038{
Junxiao Shi8c8d2182014-01-30 22:33:00 -070039 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 Afanasyev33b72772014-01-26 23:22:58 -080050}
51
Steve DiBenedetto26b730f2014-02-02 18:36:16 -070052shared_ptr<Face>
53Forwarder::getFace(FaceId id)
54{
55 std::map<FaceId, shared_ptr<Face> >::iterator i = m_faces.find(id);
56 return (i == m_faces.end()) ? (shared_ptr<Face>()) : (i->second);
57}
58
Alexander Afanasyev33b72772014-01-26 23:22:58 -080059void
Junxiao Shi8c8d2182014-01-30 22:33:00 -070060Forwarder::onInterest(Face& face, const Interest& interest)
Alexander Afanasyev33b72772014-01-26 23:22:58 -080061{
Junxiao Shi8c8d2182014-01-30 22:33:00 -070062 this->onIncomingInterest(face, interest);
63}
64
65void
66Forwarder::onData(Face& face, const Data& data)
67{
68 this->onIncomingData(face, data);
Alexander Afanasyev33b72772014-01-26 23:22:58 -080069}
70
Junxiao Shid3c792f2014-01-30 00:46:13 -070071void
72Forwarder::onIncomingInterest(Face& inFace, const Interest& interest)
73{
74 // receive Interest
Junxiao Shi8c8d2182014-01-30 22:33:00 -070075 NFD_LOG_DEBUG("onIncomingInterest face=" << inFace.getId() << " interest=" << interest.getName());
Junxiao Shi06887ac2014-02-13 20:15:42 -070076 const_cast<Interest&>(interest).setIncomingFaceId(inFace.getId());
Junxiao Shid3c792f2014-01-30 00:46:13 -070077
78 // PIT insert
79 std::pair<shared_ptr<pit::Entry>, bool>
80 pitInsertResult = m_pit.insert(interest);
81 shared_ptr<pit::Entry> pitEntry = pitInsertResult.first;
82
83 // detect loop and record Nonce
84 bool isLoop = ! pitEntry->addNonce(interest.getNonce());
85 if (isLoop) {
86 // goto Interest loop pipeline
87 this->onInterestLoop(inFace, interest, pitEntry);
88 return;
89 }
90
91 // cancel unsatisfy & straggler timer
92 this->cancelUnsatisfyAndStragglerTimer(pitEntry);
93
94 const pit::InRecordCollection& inRecords = pitEntry->getInRecords();
95 bool isPending = inRecords.begin() == inRecords.end();
96
97 if (!isPending) {
98 // CS lookup
99 const Data* csMatch = m_cs.find(interest);
100 if (csMatch != 0) {
101 // XXX should we lookup PIT for other Interests that also match csMatch?
102
103 // goto outgoing Data pipeline
104 this->onOutgoingData(*csMatch, inFace);
105 return;
106 }
107 }
108
109 // insert InRecord
110 pit::InRecordCollection::iterator inRecordIt =
111 pitEntry->insertOrUpdateInRecord(inFace.shared_from_this(), interest);
112
113 // app chosen nexthops
114 bool isAppChosenNexthops = false; // TODO get from local control header
115 if (isAppChosenNexthops) {
116 // TODO foreach chosen nexthop: goto outgoing Interest pipeline
117 return;
118 }
119
120 // FIB lookup
121 shared_ptr<fib::Entry> fibEntry
122 = m_fib.findLongestPrefixMatch(interest.getName());
123 // TODO use Fib::findParent(pitEntry)
124
125 // dispatch to strategy
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700126 m_strategy->afterReceiveInterest(inFace, interest, fibEntry, pitEntry);
127 // TODO dispatch according to fibEntry
Junxiao Shid3c792f2014-01-30 00:46:13 -0700128}
129
130void
131Forwarder::onInterestLoop(Face& inFace, const Interest& interest,
132 shared_ptr<pit::Entry> pitEntry)
133{
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700134 NFD_LOG_DEBUG("onInterestLoop face=" << inFace.getId() << " interest=" << interest.getName());
135
Junxiao Shid3c792f2014-01-30 00:46:13 -0700136 // do nothing, which means Interest is dropped
137}
138
139void
140Forwarder::onOutgoingInterest(shared_ptr<pit::Entry> pitEntry, Face& outFace)
141{
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700142 NFD_LOG_DEBUG("onOutgoingInterest face=" << outFace.getId() << " interest=" << pitEntry->getName());
143
Junxiao Shid3c792f2014-01-30 00:46:13 -0700144 // pick Interest
145 const Interest& interest = pitEntry->getInterest();
146 // TODO pick the last incoming Interest
147
148 // insert OutRecord
149 pit::OutRecordCollection::iterator outRecordIt =
150 pitEntry->insertOrUpdateOutRecord(outFace.shared_from_this(), interest);
151
152 // set PIT unsatisfy timer
153 this->setUnsatisfyTimer(pitEntry);
154
155 // send Interest
156 outFace.sendInterest(interest);
157}
158
159void
160Forwarder::onInterestRebuff(shared_ptr<pit::Entry> pitEntry)
161{
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700162 NFD_LOG_DEBUG("onInterestRebuff interest=" << pitEntry->getName());
163
Junxiao Shid3c792f2014-01-30 00:46:13 -0700164 // set PIT straggler timer
165 this->setStragglerTimer(pitEntry);
166}
167
168void
169Forwarder::onInterestUnsatisfied(shared_ptr<pit::Entry> pitEntry)
170{
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700171 NFD_LOG_DEBUG("onInterestUnsatisfied interest=" << pitEntry->getName());
172
Junxiao Shid3c792f2014-01-30 00:46:13 -0700173 // invoke PIT unsatisfied callback
174 // TODO
175
176 // PIT delete
177 m_pit.remove(pitEntry);
178}
179
180void
181Forwarder::onIncomingData(Face& inFace, const Data& data)
182{
183 // receive Data
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700184 NFD_LOG_DEBUG("onIncomingData face=" << inFace.getId() << " data=" << data.getName());
Junxiao Shi06887ac2014-02-13 20:15:42 -0700185 const_cast<Data&>(data).setIncomingFaceId(inFace.getId());
Junxiao Shid3c792f2014-01-30 00:46:13 -0700186
187 // PIT match
188 shared_ptr<pit::DataMatchResult> pitMatches = m_pit.findAllDataMatches(data);
189 if (pitMatches->begin() == pitMatches->end()) {
190 // goto Data unsolicited pipeline
191 this->onDataUnsolicited(inFace, data);
192 return;
193 }
194
195 // CS insert
196 m_cs.insert(data);
197
198 std::set<shared_ptr<Face> > pendingDownstreams;
199 // foreach PitEntry
200 for (pit::DataMatchResult::iterator it = pitMatches->begin();
201 it != pitMatches->end(); ++it) {
202 shared_ptr<pit::Entry> pitEntry = *it;
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700203 NFD_LOG_DEBUG("onIncomingData matching=" << pitEntry->getName());
Junxiao Shid3c792f2014-01-30 00:46:13 -0700204
205 // cancel unsatisfy & straggler timer
206 this->cancelUnsatisfyAndStragglerTimer(pitEntry);
207
208 // remember pending downstreams
209 const pit::InRecordCollection& inRecords = pitEntry->getInRecords();
210 for (pit::InRecordCollection::const_iterator it = inRecords.begin();
211 it != inRecords.end(); ++it) {
212 if (it->getExpiry() > time::now()) {
213 pendingDownstreams.insert(it->getFace());
214 }
215 }
216
217 // mark PIT satisfied
218 pitEntry->deleteInRecords();
219 pitEntry->deleteOutRecord(inFace.shared_from_this());
220
221 // set PIT straggler timer
222 this->setStragglerTimer(pitEntry);
223
224 // invoke PIT satisfy callback
225 // TODO
226 }
227
228 // foreach pending downstream
229 for (std::set<shared_ptr<Face> >::iterator it = pendingDownstreams.begin();
230 it != pendingDownstreams.end(); ++it) {
231 // goto outgoing Data pipeline
232 this->onOutgoingData(data, **it);
233 }
234}
235
236void
237Forwarder::onDataUnsolicited(Face& inFace, const Data& data)
238{
239 // accept to cache?
240 bool acceptToCache = false;// TODO decision
241 if (acceptToCache) {
242 // CS insert
243 m_cs.insert(data);
244 }
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700245
246 NFD_LOG_DEBUG("onDataUnsolicited face=" << inFace.getId() << " data=" << data.getName() << " acceptToCache=" << acceptToCache);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700247}
248
249void
250Forwarder::onOutgoingData(const Data& data, Face& outFace)
251{
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700252 NFD_LOG_DEBUG("onOutgoingData face=" << outFace.getId() << " data=" << data.getName());
253
Junxiao Shid3c792f2014-01-30 00:46:13 -0700254 // traffic manager
255 // pass through
256
257 // send Data
258 outFace.sendData(data);
259}
260
261static inline bool
262compare_InRecord_expiry(const pit::InRecord& a, const pit::InRecord& b)
263{
264 return a.getExpiry() < b.getExpiry();
265}
266
267void
268Forwarder::setUnsatisfyTimer(shared_ptr<pit::Entry> pitEntry)
269{
270 const pit::InRecordCollection& inRecords = pitEntry->getInRecords();
271 pit::InRecordCollection::const_iterator lastExpiring =
272 std::max_element(inRecords.begin(), inRecords.end(),
273 &compare_InRecord_expiry);
274
275 time::Point lastExpiry = lastExpiring->getExpiry();
276 time::Duration lastExpiryFromNow = lastExpiry - time::now();
277 if (lastExpiryFromNow <= time::seconds(0)) {
278 // TODO all InRecords are already expired; will this happen?
279 }
280
281 pitEntry->m_unsatisfyTimer = m_scheduler.scheduleEvent(lastExpiryFromNow,
282 bind(&Forwarder::onInterestUnsatisfied, this, pitEntry));
283}
284
285void
286Forwarder::setStragglerTimer(shared_ptr<pit::Entry> pitEntry)
287{
288 time::Duration stragglerTime = time::milliseconds(100);
289
290 pitEntry->m_stragglerTimer = m_scheduler.scheduleEvent(stragglerTime,
291 bind(&Pit::remove, &m_pit, pitEntry));
292}
293
294void
295Forwarder::cancelUnsatisfyAndStragglerTimer(shared_ptr<pit::Entry> pitEntry)
296{
297 m_scheduler.cancelEvent(pitEntry->m_unsatisfyTimer);
298 m_scheduler.cancelEvent(pitEntry->m_stragglerTimer);
299}
300
301
302
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800303} // namespace nfd