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