blob: b32041e864b315dee1c2551dc6ce251149afc746 [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
Junxiao Shic041ca32014-02-25 20:01:15 -070017Forwarder::Forwarder()
Junxiao Shia4f2be82014-03-02 22:56:41 -070018 : m_faceTable(*this)
Haowei Yuan78c84d12014-02-27 15:35:13 -060019 , m_nameTree(1024) // "1024" could be made as one configurable parameter of the forwarder.
HangZhangad4afd12014-03-01 11:03:08 +080020 , m_fib(m_nameTree)
Haowei Yuan78c84d12014-02-27 15:35:13 -060021 , m_pit(m_nameTree)
HangZhangc85a23c2014-03-01 15:55:55 +080022 , m_measurements(m_nameTree)
Alexander Afanasyev33b72772014-01-26 23:22:58 -080023{
Junxiao Shi8c8d2182014-01-30 22:33:00 -070024 m_strategy = make_shared<fw::BestRouteStrategy>(boost::ref(*this));
Alexander Afanasyev33b72772014-01-26 23:22:58 -080025}
26
27void
Junxiao Shid3c792f2014-01-30 00:46:13 -070028Forwarder::onIncomingInterest(Face& inFace, const Interest& interest)
29{
30 // receive Interest
Junxiao Shi8c8d2182014-01-30 22:33:00 -070031 NFD_LOG_DEBUG("onIncomingInterest face=" << inFace.getId() << " interest=" << interest.getName());
Junxiao Shi06887ac2014-02-13 20:15:42 -070032 const_cast<Interest&>(interest).setIncomingFaceId(inFace.getId());
Junxiao Shic041ca32014-02-25 20:01:15 -070033
Junxiao Shi88884492014-02-15 15:57:43 -070034 // /localhost scope control
35 bool violatesLocalhost = !inFace.isLocal() &&
36 s_localhostName.isPrefixOf(interest.getName());
37 if (violatesLocalhost) {
38 NFD_LOG_DEBUG("onIncomingInterest face=" << inFace.getId()
39 << " interest=" << interest.getName() << " violates /localhost");
40 return;
41 }
Junxiao Shic041ca32014-02-25 20:01:15 -070042
Junxiao Shid3c792f2014-01-30 00:46:13 -070043 // PIT insert
Junxiao Shi40631842014-03-01 13:52:37 -070044 shared_ptr<pit::Entry> pitEntry = m_pit.insert(interest).first;
Junxiao Shic041ca32014-02-25 20:01:15 -070045
Junxiao Shid3c792f2014-01-30 00:46:13 -070046 // detect loop and record Nonce
47 bool isLoop = ! pitEntry->addNonce(interest.getNonce());
48 if (isLoop) {
49 // goto Interest loop pipeline
50 this->onInterestLoop(inFace, interest, pitEntry);
51 return;
52 }
Junxiao Shic041ca32014-02-25 20:01:15 -070053
Junxiao Shid3c792f2014-01-30 00:46:13 -070054 // cancel unsatisfy & straggler timer
55 this->cancelUnsatisfyAndStragglerTimer(pitEntry);
Junxiao Shic041ca32014-02-25 20:01:15 -070056
Junxiao Shid3c792f2014-01-30 00:46:13 -070057 const pit::InRecordCollection& inRecords = pitEntry->getInRecords();
58 bool isPending = inRecords.begin() == inRecords.end();
Junxiao Shic041ca32014-02-25 20:01:15 -070059
Junxiao Shid3c792f2014-01-30 00:46:13 -070060 if (!isPending) {
61 // CS lookup
62 const Data* csMatch = m_cs.find(interest);
63 if (csMatch != 0) {
64 // XXX should we lookup PIT for other Interests that also match csMatch?
65
66 // goto outgoing Data pipeline
67 this->onOutgoingData(*csMatch, inFace);
68 return;
69 }
70 }
Junxiao Shic041ca32014-02-25 20:01:15 -070071
Junxiao Shid3c792f2014-01-30 00:46:13 -070072 // insert InRecord
Junxiao Shi9b27bd22014-02-26 20:29:58 -070073 pitEntry->insertOrUpdateInRecord(inFace.shared_from_this(), interest);
Junxiao Shic041ca32014-02-25 20:01:15 -070074
Junxiao Shid3c792f2014-01-30 00:46:13 -070075 // app chosen nexthops
76 bool isAppChosenNexthops = false; // TODO get from local control header
77 if (isAppChosenNexthops) {
78 // TODO foreach chosen nexthop: goto outgoing Interest pipeline
79 return;
80 }
Junxiao Shic041ca32014-02-25 20:01:15 -070081
Junxiao Shid3c792f2014-01-30 00:46:13 -070082 // FIB lookup
Junxiao Shi40631842014-03-01 13:52:37 -070083 shared_ptr<fib::Entry> fibEntry = m_fib.findLongestPrefixMatch(*pitEntry);
Junxiao Shic041ca32014-02-25 20:01:15 -070084
Junxiao Shid3c792f2014-01-30 00:46:13 -070085 // dispatch to strategy
Junxiao Shi88884492014-02-15 15:57:43 -070086 this->dispatchToStrategy(inFace, interest, fibEntry, pitEntry);
Junxiao Shid3c792f2014-01-30 00:46:13 -070087}
88
89void
90Forwarder::onInterestLoop(Face& inFace, const Interest& interest,
91 shared_ptr<pit::Entry> pitEntry)
92{
Junxiao Shi8c8d2182014-01-30 22:33:00 -070093 NFD_LOG_DEBUG("onInterestLoop face=" << inFace.getId() << " interest=" << interest.getName());
94
Junxiao Shid3c792f2014-01-30 00:46:13 -070095 // do nothing, which means Interest is dropped
96}
97
98void
99Forwarder::onOutgoingInterest(shared_ptr<pit::Entry> pitEntry, Face& outFace)
100{
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700101 NFD_LOG_DEBUG("onOutgoingInterest face=" << outFace.getId() << " interest=" << pitEntry->getName());
102
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700103 // /localhost scope control
104 bool violatesLocalhost = !outFace.isLocal() &&
105 s_localhostName.isPrefixOf(pitEntry->getName());
106 if (violatesLocalhost) {
107 NFD_LOG_DEBUG("onOutgoingInterest face=" << outFace.getId()
108 << " interest=" << pitEntry->getName() << " violates /localhost");
109 return;
110 }
111
Junxiao Shid3c792f2014-01-30 00:46:13 -0700112 // pick Interest
113 const Interest& interest = pitEntry->getInterest();
114 // TODO pick the last incoming Interest
Junxiao Shic041ca32014-02-25 20:01:15 -0700115
Junxiao Shid3c792f2014-01-30 00:46:13 -0700116 // insert OutRecord
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700117 pitEntry->insertOrUpdateOutRecord(outFace.shared_from_this(), interest);
Junxiao Shic041ca32014-02-25 20:01:15 -0700118
Junxiao Shid3c792f2014-01-30 00:46:13 -0700119 // set PIT unsatisfy timer
120 this->setUnsatisfyTimer(pitEntry);
Junxiao Shic041ca32014-02-25 20:01:15 -0700121
Junxiao Shid3c792f2014-01-30 00:46:13 -0700122 // send Interest
123 outFace.sendInterest(interest);
124}
125
126void
Junxiao Shi09498f02014-02-26 19:41:08 -0700127Forwarder::onInterestReject(shared_ptr<pit::Entry> pitEntry)
Junxiao Shid3c792f2014-01-30 00:46:13 -0700128{
Junxiao Shi09498f02014-02-26 19:41:08 -0700129 NFD_LOG_DEBUG("onInterestReject interest=" << pitEntry->getName());
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700130
Junxiao Shid3c792f2014-01-30 00:46:13 -0700131 // set PIT straggler timer
132 this->setStragglerTimer(pitEntry);
133}
134
135void
136Forwarder::onInterestUnsatisfied(shared_ptr<pit::Entry> pitEntry)
137{
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700138 NFD_LOG_DEBUG("onInterestUnsatisfied interest=" << pitEntry->getName());
139
Junxiao Shid3c792f2014-01-30 00:46:13 -0700140 // invoke PIT unsatisfied callback
141 // TODO
Junxiao Shic041ca32014-02-25 20:01:15 -0700142
Haowei Yuan78c84d12014-02-27 15:35:13 -0600143 // PIT erase
144 m_pit.erase(pitEntry);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700145}
146
147void
148Forwarder::onIncomingData(Face& inFace, const Data& data)
149{
150 // receive Data
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700151 NFD_LOG_DEBUG("onIncomingData face=" << inFace.getId() << " data=" << data.getName());
Junxiao Shi06887ac2014-02-13 20:15:42 -0700152 const_cast<Data&>(data).setIncomingFaceId(inFace.getId());
Junxiao Shic041ca32014-02-25 20:01:15 -0700153
Junxiao Shi88884492014-02-15 15:57:43 -0700154 // /localhost scope control
155 bool violatesLocalhost = !inFace.isLocal() &&
156 s_localhostName.isPrefixOf(data.getName());
157 if (violatesLocalhost) {
158 NFD_LOG_DEBUG("onIncomingData face=" << inFace.getId()
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700159 << " data=" << data.getName() << " violates /localhost");
Junxiao Shi88884492014-02-15 15:57:43 -0700160 return;
161 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700162
Junxiao Shid3c792f2014-01-30 00:46:13 -0700163 // PIT match
164 shared_ptr<pit::DataMatchResult> pitMatches = m_pit.findAllDataMatches(data);
165 if (pitMatches->begin() == pitMatches->end()) {
166 // goto Data unsolicited pipeline
167 this->onDataUnsolicited(inFace, data);
168 return;
169 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700170
Junxiao Shid3c792f2014-01-30 00:46:13 -0700171 // CS insert
172 m_cs.insert(data);
Junxiao Shic041ca32014-02-25 20:01:15 -0700173
Junxiao Shid3c792f2014-01-30 00:46:13 -0700174 std::set<shared_ptr<Face> > pendingDownstreams;
175 // foreach PitEntry
176 for (pit::DataMatchResult::iterator it = pitMatches->begin();
177 it != pitMatches->end(); ++it) {
178 shared_ptr<pit::Entry> pitEntry = *it;
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700179 NFD_LOG_DEBUG("onIncomingData matching=" << pitEntry->getName());
Junxiao Shic041ca32014-02-25 20:01:15 -0700180
Junxiao Shid3c792f2014-01-30 00:46:13 -0700181 // cancel unsatisfy & straggler timer
182 this->cancelUnsatisfyAndStragglerTimer(pitEntry);
Junxiao Shic041ca32014-02-25 20:01:15 -0700183
Junxiao Shid3c792f2014-01-30 00:46:13 -0700184 // remember pending downstreams
185 const pit::InRecordCollection& inRecords = pitEntry->getInRecords();
186 for (pit::InRecordCollection::const_iterator it = inRecords.begin();
187 it != inRecords.end(); ++it) {
188 if (it->getExpiry() > time::now()) {
189 pendingDownstreams.insert(it->getFace());
190 }
191 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700192
Junxiao Shid3c792f2014-01-30 00:46:13 -0700193 // mark PIT satisfied
194 pitEntry->deleteInRecords();
195 pitEntry->deleteOutRecord(inFace.shared_from_this());
Junxiao Shic041ca32014-02-25 20:01:15 -0700196
Junxiao Shid3c792f2014-01-30 00:46:13 -0700197 // set PIT straggler timer
198 this->setStragglerTimer(pitEntry);
Junxiao Shic041ca32014-02-25 20:01:15 -0700199
Junxiao Shid3c792f2014-01-30 00:46:13 -0700200 // invoke PIT satisfy callback
201 // TODO
202 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700203
Junxiao Shid3c792f2014-01-30 00:46:13 -0700204 // foreach pending downstream
205 for (std::set<shared_ptr<Face> >::iterator it = pendingDownstreams.begin();
206 it != pendingDownstreams.end(); ++it) {
207 // goto outgoing Data pipeline
208 this->onOutgoingData(data, **it);
209 }
210}
211
212void
213Forwarder::onDataUnsolicited(Face& inFace, const Data& data)
214{
215 // accept to cache?
216 bool acceptToCache = false;// TODO decision
217 if (acceptToCache) {
218 // CS insert
219 m_cs.insert(data);
220 }
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700221
222 NFD_LOG_DEBUG("onDataUnsolicited face=" << inFace.getId() << " data=" << data.getName() << " acceptToCache=" << acceptToCache);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700223}
224
225void
226Forwarder::onOutgoingData(const Data& data, Face& outFace)
227{
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700228 NFD_LOG_DEBUG("onOutgoingData face=" << outFace.getId() << " data=" << data.getName());
229
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700230 // /localhost scope control
231 bool violatesLocalhost = !outFace.isLocal() &&
232 s_localhostName.isPrefixOf(data.getName());
233 if (violatesLocalhost) {
234 NFD_LOG_DEBUG("onOutgoingData face=" << outFace.getId()
235 << " data=" << data.getName() << " violates /localhost");
236 return;
237 }
238
Junxiao Shid3c792f2014-01-30 00:46:13 -0700239 // traffic manager
240 // pass through
Junxiao Shic041ca32014-02-25 20:01:15 -0700241
Junxiao Shid3c792f2014-01-30 00:46:13 -0700242 // send Data
243 outFace.sendData(data);
244}
245
246static inline bool
247compare_InRecord_expiry(const pit::InRecord& a, const pit::InRecord& b)
248{
249 return a.getExpiry() < b.getExpiry();
250}
251
252void
253Forwarder::setUnsatisfyTimer(shared_ptr<pit::Entry> pitEntry)
254{
255 const pit::InRecordCollection& inRecords = pitEntry->getInRecords();
256 pit::InRecordCollection::const_iterator lastExpiring =
257 std::max_element(inRecords.begin(), inRecords.end(),
258 &compare_InRecord_expiry);
259
260 time::Point lastExpiry = lastExpiring->getExpiry();
261 time::Duration lastExpiryFromNow = lastExpiry - time::now();
262 if (lastExpiryFromNow <= time::seconds(0)) {
263 // TODO all InRecords are already expired; will this happen?
264 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700265
266 pitEntry->m_unsatisfyTimer = scheduler::schedule(lastExpiryFromNow,
Junxiao Shid3c792f2014-01-30 00:46:13 -0700267 bind(&Forwarder::onInterestUnsatisfied, this, pitEntry));
268}
269
270void
271Forwarder::setStragglerTimer(shared_ptr<pit::Entry> pitEntry)
272{
273 time::Duration stragglerTime = time::milliseconds(100);
Junxiao Shic041ca32014-02-25 20:01:15 -0700274
275 pitEntry->m_stragglerTimer = scheduler::schedule(stragglerTime,
Haowei Yuan78c84d12014-02-27 15:35:13 -0600276 bind(&Pit::erase, &m_pit, pitEntry));
Junxiao Shid3c792f2014-01-30 00:46:13 -0700277}
278
279void
280Forwarder::cancelUnsatisfyAndStragglerTimer(shared_ptr<pit::Entry> pitEntry)
281{
Junxiao Shic041ca32014-02-25 20:01:15 -0700282 scheduler::cancel(pitEntry->m_unsatisfyTimer);
283 scheduler::cancel(pitEntry->m_stragglerTimer);
Junxiao Shid3c792f2014-01-30 00:46:13 -0700284}
285
Junxiao Shi88884492014-02-15 15:57:43 -0700286void
287Forwarder::dispatchToStrategy(const Face& inFace,
288 const Interest& interest,
289 shared_ptr<fib::Entry> fibEntry,
290 shared_ptr<pit::Entry> pitEntry)
291{
292 m_strategy->afterReceiveInterest(inFace, interest, fibEntry, pitEntry);
293 // TODO dispatch according to fibEntry
294}
Junxiao Shid3c792f2014-01-30 00:46:13 -0700295
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800296} // namespace nfd