blob: 1ce3b8f8503e2f05f0fbae5e7446b2f7c6f5eda8 [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 Shid3c792f2014-01-30 00:46:13 -070076
77 // PIT insert
78 std::pair<shared_ptr<pit::Entry>, bool>
79 pitInsertResult = m_pit.insert(interest);
80 shared_ptr<pit::Entry> pitEntry = pitInsertResult.first;
81
82 // detect loop and record Nonce
83 bool isLoop = ! pitEntry->addNonce(interest.getNonce());
84 if (isLoop) {
85 // goto Interest loop pipeline
86 this->onInterestLoop(inFace, interest, pitEntry);
87 return;
88 }
89
90 // cancel unsatisfy & straggler timer
91 this->cancelUnsatisfyAndStragglerTimer(pitEntry);
92
93 const pit::InRecordCollection& inRecords = pitEntry->getInRecords();
94 bool isPending = inRecords.begin() == inRecords.end();
95
96 if (!isPending) {
97 // CS lookup
98 const Data* csMatch = m_cs.find(interest);
99 if (csMatch != 0) {
100 // XXX should we lookup PIT for other Interests that also match csMatch?
101
102 // goto outgoing Data pipeline
103 this->onOutgoingData(*csMatch, inFace);
104 return;
105 }
106 }
107
108 // insert InRecord
109 pit::InRecordCollection::iterator inRecordIt =
110 pitEntry->insertOrUpdateInRecord(inFace.shared_from_this(), interest);
111
112 // app chosen nexthops
113 bool isAppChosenNexthops = false; // TODO get from local control header
114 if (isAppChosenNexthops) {
115 // TODO foreach chosen nexthop: goto outgoing Interest pipeline
116 return;
117 }
118
119 // FIB lookup
120 shared_ptr<fib::Entry> fibEntry
121 = m_fib.findLongestPrefixMatch(interest.getName());
122 // TODO use Fib::findParent(pitEntry)
123
124 // dispatch to strategy
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700125 m_strategy->afterReceiveInterest(inFace, interest, fibEntry, pitEntry);
126 // TODO dispatch according to fibEntry
Junxiao Shid3c792f2014-01-30 00:46:13 -0700127}
128
129void
130Forwarder::onInterestLoop(Face& inFace, const Interest& interest,
131 shared_ptr<pit::Entry> pitEntry)
132{
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700133 NFD_LOG_DEBUG("onInterestLoop face=" << inFace.getId() << " interest=" << interest.getName());
134
Junxiao Shid3c792f2014-01-30 00:46:13 -0700135 // do nothing, which means Interest is dropped
136}
137
138void
139Forwarder::onOutgoingInterest(shared_ptr<pit::Entry> pitEntry, Face& outFace)
140{
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700141 NFD_LOG_DEBUG("onOutgoingInterest face=" << outFace.getId() << " interest=" << pitEntry->getName());
142
Junxiao Shid3c792f2014-01-30 00:46:13 -0700143 // pick Interest
144 const Interest& interest = pitEntry->getInterest();
145 // TODO pick the last incoming Interest
146
147 // insert OutRecord
148 pit::OutRecordCollection::iterator outRecordIt =
149 pitEntry->insertOrUpdateOutRecord(outFace.shared_from_this(), interest);
150
151 // set PIT unsatisfy timer
152 this->setUnsatisfyTimer(pitEntry);
153
154 // send Interest
155 outFace.sendInterest(interest);
156}
157
158void
159Forwarder::onInterestRebuff(shared_ptr<pit::Entry> pitEntry)
160{
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700161 NFD_LOG_DEBUG("onInterestRebuff interest=" << pitEntry->getName());
162
Junxiao Shid3c792f2014-01-30 00:46:13 -0700163 // set PIT straggler timer
164 this->setStragglerTimer(pitEntry);
165}
166
167void
168Forwarder::onInterestUnsatisfied(shared_ptr<pit::Entry> pitEntry)
169{
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700170 NFD_LOG_DEBUG("onInterestUnsatisfied interest=" << pitEntry->getName());
171
Junxiao Shid3c792f2014-01-30 00:46:13 -0700172 // invoke PIT unsatisfied callback
173 // TODO
174
175 // PIT delete
176 m_pit.remove(pitEntry);
177}
178
179void
180Forwarder::onIncomingData(Face& inFace, const Data& data)
181{
182 // receive Data
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700183 NFD_LOG_DEBUG("onIncomingData face=" << inFace.getId() << " data=" << data.getName());
Junxiao Shid3c792f2014-01-30 00:46:13 -0700184
185 // PIT match
186 shared_ptr<pit::DataMatchResult> pitMatches = m_pit.findAllDataMatches(data);
187 if (pitMatches->begin() == pitMatches->end()) {
188 // goto Data unsolicited pipeline
189 this->onDataUnsolicited(inFace, data);
190 return;
191 }
192
193 // CS insert
194 m_cs.insert(data);
195
196 std::set<shared_ptr<Face> > pendingDownstreams;
197 // foreach PitEntry
198 for (pit::DataMatchResult::iterator it = pitMatches->begin();
199 it != pitMatches->end(); ++it) {
200 shared_ptr<pit::Entry> pitEntry = *it;
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700201 NFD_LOG_DEBUG("onIncomingData matching=" << pitEntry->getName());
Junxiao Shid3c792f2014-01-30 00:46:13 -0700202
203 // cancel unsatisfy & straggler timer
204 this->cancelUnsatisfyAndStragglerTimer(pitEntry);
205
206 // remember pending downstreams
207 const pit::InRecordCollection& inRecords = pitEntry->getInRecords();
208 for (pit::InRecordCollection::const_iterator it = inRecords.begin();
209 it != inRecords.end(); ++it) {
210 if (it->getExpiry() > time::now()) {
211 pendingDownstreams.insert(it->getFace());
212 }
213 }
214
215 // mark PIT satisfied
216 pitEntry->deleteInRecords();
217 pitEntry->deleteOutRecord(inFace.shared_from_this());
218
219 // set PIT straggler timer
220 this->setStragglerTimer(pitEntry);
221
222 // invoke PIT satisfy callback
223 // TODO
224 }
225
226 // foreach pending downstream
227 for (std::set<shared_ptr<Face> >::iterator it = pendingDownstreams.begin();
228 it != pendingDownstreams.end(); ++it) {
229 // goto outgoing Data pipeline
230 this->onOutgoingData(data, **it);
231 }
232}
233
234void
235Forwarder::onDataUnsolicited(Face& inFace, const Data& data)
236{
237 // accept to cache?
238 bool acceptToCache = false;// TODO decision
239 if (acceptToCache) {
240 // CS insert
241 m_cs.insert(data);
242 }
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700243
244 NFD_LOG_DEBUG("onDataUnsolicited face=" << inFace.getId() << " data=" << data.getName() << " acceptToCache=" << acceptToCache);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700245}
246
247void
248Forwarder::onOutgoingData(const Data& data, Face& outFace)
249{
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700250 NFD_LOG_DEBUG("onOutgoingData face=" << outFace.getId() << " data=" << data.getName());
251
Junxiao Shid3c792f2014-01-30 00:46:13 -0700252 // traffic manager
253 // pass through
254
255 // send Data
256 outFace.sendData(data);
257}
258
259static inline bool
260compare_InRecord_expiry(const pit::InRecord& a, const pit::InRecord& b)
261{
262 return a.getExpiry() < b.getExpiry();
263}
264
265void
266Forwarder::setUnsatisfyTimer(shared_ptr<pit::Entry> pitEntry)
267{
268 const pit::InRecordCollection& inRecords = pitEntry->getInRecords();
269 pit::InRecordCollection::const_iterator lastExpiring =
270 std::max_element(inRecords.begin(), inRecords.end(),
271 &compare_InRecord_expiry);
272
273 time::Point lastExpiry = lastExpiring->getExpiry();
274 time::Duration lastExpiryFromNow = lastExpiry - time::now();
275 if (lastExpiryFromNow <= time::seconds(0)) {
276 // TODO all InRecords are already expired; will this happen?
277 }
278
279 pitEntry->m_unsatisfyTimer = m_scheduler.scheduleEvent(lastExpiryFromNow,
280 bind(&Forwarder::onInterestUnsatisfied, this, pitEntry));
281}
282
283void
284Forwarder::setStragglerTimer(shared_ptr<pit::Entry> pitEntry)
285{
286 time::Duration stragglerTime = time::milliseconds(100);
287
288 pitEntry->m_stragglerTimer = m_scheduler.scheduleEvent(stragglerTime,
289 bind(&Pit::remove, &m_pit, pitEntry));
290}
291
292void
293Forwarder::cancelUnsatisfyAndStragglerTimer(shared_ptr<pit::Entry> pitEntry)
294{
295 m_scheduler.cancelEvent(pitEntry->m_unsatisfyTimer);
296 m_scheduler.cancelEvent(pitEntry->m_stragglerTimer);
297}
298
299
300
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800301} // namespace nfd