blob: 574688f1a5f5182ce0f51d23fa8dbba04fe9a901 [file] [log] [blame]
Alexander Afanasyev33b72772014-01-26 23:22:58 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shia110f262014-10-12 12:35:20 -07003 * Copyright (c) 2014, Regents of the University of California,
4 * Arizona Board of Regents,
5 * Colorado State University,
6 * University Pierre & Marie Curie, Sorbonne University,
7 * Washington University in St. Louis,
8 * Beijing Institute of Technology,
9 * The University of Memphis
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070010 *
11 * This file is part of NFD (Named Data Networking Forwarding Daemon).
12 * See AUTHORS.md for complete list of NFD authors and contributors.
13 *
14 * NFD is free software: you can redistribute it and/or modify it under the terms
15 * of the GNU General Public License as published by the Free Software Foundation,
16 * either version 3 of the License, or (at your option) any later version.
17 *
18 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
19 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
20 * PURPOSE. See the GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along with
23 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
Junxiao Shia110f262014-10-12 12:35:20 -070024 */
Alexander Afanasyev33b72772014-01-26 23:22:58 -080025
Alexander Afanasyev613e2a92014-04-15 13:36:58 -070026#ifndef NFD_DAEMON_FW_FORWARDER_HPP
27#define NFD_DAEMON_FW_FORWARDER_HPP
Alexander Afanasyev33b72772014-01-26 23:22:58 -080028
29#include "common.hpp"
Junxiao Shid3c792f2014-01-30 00:46:13 -070030#include "core/scheduler.hpp"
Junxiao Shi33152f12014-07-16 19:54:32 -070031#include "forwarder-counters.hpp"
Junxiao Shia4f2be82014-03-02 22:56:41 -070032#include "face-table.hpp"
Junxiao Shid3c792f2014-01-30 00:46:13 -070033#include "table/fib.hpp"
34#include "table/pit.hpp"
35#include "table/cs.hpp"
Junxiao Shidbe71732014-02-21 22:23:28 -070036#include "table/measurements.hpp"
Junxiao Shibb5105f2014-03-03 12:06:45 -070037#include "table/strategy-choice.hpp"
Junxiao Shia110f262014-10-12 12:35:20 -070038#include "table/dead-nonce-list.hpp"
Alexander Afanasyev33b72772014-01-26 23:22:58 -080039
Spyridon Mastorakisde1f7732014-12-05 22:43:34 -080040#include "ns3/ndnSIM/model/cs/ndn-content-store.hpp"
41
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080042namespace nfd {
Alexander Afanasyev33b72772014-01-26 23:22:58 -080043
Junxiao Shi2d9bdc82014-03-02 20:55:42 -070044namespace fw {
45class Strategy;
46} // namespace fw
47
Alexander Afanasyev9bcf39e2015-01-08 21:41:48 -080048class NullFace;
49
Junxiao Shi2d9bdc82014-03-02 20:55:42 -070050/** \brief main class of NFD
Junxiao Shic041ca32014-02-25 20:01:15 -070051 *
Junxiao Shi2d9bdc82014-03-02 20:55:42 -070052 * Forwarder owns all faces and tables, and implements forwarding pipelines.
Alexander Afanasyev33b72772014-01-26 23:22:58 -080053 */
54class Forwarder
55{
56public:
Junxiao Shic041ca32014-02-25 20:01:15 -070057 Forwarder();
Alexander Afanasyev33b72772014-01-26 23:22:58 -080058
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060059 VIRTUAL_WITH_TESTS
60 ~Forwarder();
61
Junxiao Shib289cc12014-03-15 12:19:05 -070062 const ForwarderCounters&
63 getCounters() const;
64
Junxiao Shia4f2be82014-03-02 22:56:41 -070065public: // faces
66 FaceTable&
67 getFaceTable();
68
69 /** \brief get existing Face
70 *
71 * shortcut to .getFaceTable().get(face)
72 */
73 shared_ptr<Face>
74 getFace(FaceId id) const;
75
76 /** \brief add new Face
77 *
78 * shortcut to .getFaceTable().add(face)
79 */
Junxiao Shi8c8d2182014-01-30 22:33:00 -070080 void
81 addFace(shared_ptr<Face> face);
Alexander Afanasyev33b72772014-01-26 23:22:58 -080082
Junxiao Shia4f2be82014-03-02 22:56:41 -070083public: // forwarding entrypoints and tables
Alexander Afanasyev33b72772014-01-26 23:22:58 -080084 void
Junxiao Shi8c8d2182014-01-30 22:33:00 -070085 onInterest(Face& face, const Interest& interest);
Alexander Afanasyev33b72772014-01-26 23:22:58 -080086
87 void
Junxiao Shi8c8d2182014-01-30 22:33:00 -070088 onData(Face& face, const Data& data);
Junxiao Shic041ca32014-02-25 20:01:15 -070089
Junxiao Shiea48d8b2014-03-16 13:53:47 -070090 NameTree&
91 getNameTree();
92
Junxiao Shi8c8d2182014-01-30 22:33:00 -070093 Fib&
94 getFib();
Junxiao Shic041ca32014-02-25 20:01:15 -070095
Junxiao Shi8c8d2182014-01-30 22:33:00 -070096 Pit&
97 getPit();
Junxiao Shic041ca32014-02-25 20:01:15 -070098
Junxiao Shi8c8d2182014-01-30 22:33:00 -070099 Cs&
100 getCs();
Junxiao Shic041ca32014-02-25 20:01:15 -0700101
Junxiao Shidbe71732014-02-21 22:23:28 -0700102 Measurements&
103 getMeasurements();
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700104
Junxiao Shibb5105f2014-03-03 12:06:45 -0700105 StrategyChoice&
106 getStrategyChoice();
107
Junxiao Shia110f262014-10-12 12:35:20 -0700108 DeadNonceList&
109 getDeadNonceList();
110
Spyridon Mastorakisde1f7732014-12-05 22:43:34 -0800111public: // allow enabling ndnSIM content store (will be removed in the future)
112 void
113 setCsFromNdnSim(ns3::Ptr<ns3::ndn::ContentStore> cs);
114
Alexander Afanasyev9bcf39e2015-01-08 21:41:48 -0800115public:
116 /** \brief trigger before PIT entry is satisfied
117 * \sa Strategy::beforeSatisfyInterest
118 */
119 signal::Signal<Forwarder, pit::Entry, Face, Data> beforeSatisfyInterest;
120
121 /** \brief trigger before PIT entry expires
122 * \sa Strategy::beforeExpirePendingInterest
123 */
124 signal::Signal<Forwarder, pit::Entry> beforeExpirePendingInterest;
125
Junxiao Shi88884492014-02-15 15:57:43 -0700126PUBLIC_WITH_TESTS_ELSE_PRIVATE: // pipelines
Junxiao Shid3c792f2014-01-30 00:46:13 -0700127 /** \brief incoming Interest pipeline
128 */
Junxiao Shi88884492014-02-15 15:57:43 -0700129 VIRTUAL_WITH_TESTS void
Junxiao Shid3c792f2014-01-30 00:46:13 -0700130 onIncomingInterest(Face& inFace, const Interest& interest);
131
132 /** \brief Interest loop pipeline
133 */
Junxiao Shi88884492014-02-15 15:57:43 -0700134 VIRTUAL_WITH_TESTS void
Junxiao Shid3c792f2014-01-30 00:46:13 -0700135 onInterestLoop(Face& inFace, const Interest& interest,
136 shared_ptr<pit::Entry> pitEntry);
Junxiao Shic041ca32014-02-25 20:01:15 -0700137
Junxiao Shid3c792f2014-01-30 00:46:13 -0700138 /** \brief outgoing Interest pipeline
139 */
Junxiao Shi88884492014-02-15 15:57:43 -0700140 VIRTUAL_WITH_TESTS void
Junxiao Shid938a6b2014-05-11 23:40:29 -0700141 onOutgoingInterest(shared_ptr<pit::Entry> pitEntry, Face& outFace,
142 bool wantNewNonce = false);
Junxiao Shic041ca32014-02-25 20:01:15 -0700143
Junxiao Shi09498f02014-02-26 19:41:08 -0700144 /** \brief Interest reject pipeline
Junxiao Shid3c792f2014-01-30 00:46:13 -0700145 */
Junxiao Shi88884492014-02-15 15:57:43 -0700146 VIRTUAL_WITH_TESTS void
Junxiao Shi09498f02014-02-26 19:41:08 -0700147 onInterestReject(shared_ptr<pit::Entry> pitEntry);
Junxiao Shic041ca32014-02-25 20:01:15 -0700148
Junxiao Shid3c792f2014-01-30 00:46:13 -0700149 /** \brief Interest unsatisfied pipeline
150 */
Junxiao Shi88884492014-02-15 15:57:43 -0700151 VIRTUAL_WITH_TESTS void
Junxiao Shid3c792f2014-01-30 00:46:13 -0700152 onInterestUnsatisfied(shared_ptr<pit::Entry> pitEntry);
Junxiao Shic041ca32014-02-25 20:01:15 -0700153
Junxiao Shia110f262014-10-12 12:35:20 -0700154 /** \brief Interest finalize pipeline
155 * \param isSatisfied whether the Interest has been satisfied
156 * \param dataFreshnessPeriod FreshnessPeriod of satisfying Data
157 */
158 VIRTUAL_WITH_TESTS void
159 onInterestFinalize(shared_ptr<pit::Entry> pitEntry, bool isSatisfied,
160 const time::milliseconds& dataFreshnessPeriod = time::milliseconds(-1));
161
Junxiao Shid3c792f2014-01-30 00:46:13 -0700162 /** \brief incoming Data pipeline
163 */
Junxiao Shi88884492014-02-15 15:57:43 -0700164 VIRTUAL_WITH_TESTS void
Junxiao Shid3c792f2014-01-30 00:46:13 -0700165 onIncomingData(Face& inFace, const Data& data);
Junxiao Shic041ca32014-02-25 20:01:15 -0700166
Junxiao Shid3c792f2014-01-30 00:46:13 -0700167 /** \brief Data unsolicited pipeline
168 */
Junxiao Shi88884492014-02-15 15:57:43 -0700169 VIRTUAL_WITH_TESTS void
Junxiao Shid3c792f2014-01-30 00:46:13 -0700170 onDataUnsolicited(Face& inFace, const Data& data);
Junxiao Shic041ca32014-02-25 20:01:15 -0700171
Junxiao Shid3c792f2014-01-30 00:46:13 -0700172 /** \brief outgoing Data pipeline
173 */
Junxiao Shi88884492014-02-15 15:57:43 -0700174 VIRTUAL_WITH_TESTS void
Junxiao Shid3c792f2014-01-30 00:46:13 -0700175 onOutgoingData(const Data& data, Face& outFace);
176
Junxiao Shi88884492014-02-15 15:57:43 -0700177PROTECTED_WITH_TESTS_ELSE_PRIVATE:
178 VIRTUAL_WITH_TESTS void
Junxiao Shid3c792f2014-01-30 00:46:13 -0700179 setUnsatisfyTimer(shared_ptr<pit::Entry> pitEntry);
Junxiao Shic041ca32014-02-25 20:01:15 -0700180
Junxiao Shi88884492014-02-15 15:57:43 -0700181 VIRTUAL_WITH_TESTS void
Junxiao Shia110f262014-10-12 12:35:20 -0700182 setStragglerTimer(shared_ptr<pit::Entry> pitEntry, bool isSatisfied,
183 const time::milliseconds& dataFreshnessPeriod = time::milliseconds(-1));
Junxiao Shic041ca32014-02-25 20:01:15 -0700184
Junxiao Shi88884492014-02-15 15:57:43 -0700185 VIRTUAL_WITH_TESTS void
Junxiao Shid3c792f2014-01-30 00:46:13 -0700186 cancelUnsatisfyAndStragglerTimer(shared_ptr<pit::Entry> pitEntry);
Junxiao Shic041ca32014-02-25 20:01:15 -0700187
Junxiao Shia110f262014-10-12 12:35:20 -0700188 /** \brief insert Nonce to Dead Nonce List if necessary
189 * \param upstream if null, insert Nonces from all OutRecords;
190 * if not null, insert Nonce only on the OutRecord of this face
191 */
192 VIRTUAL_WITH_TESTS void
193 insertDeadNonceList(pit::Entry& pitEntry, bool isSatisfied,
194 const time::milliseconds& dataFreshnessPeriod,
195 Face* upstream);
196
Junxiao Shif3c07812014-03-11 21:48:49 -0700197 /// call trigger (method) on the effective strategy of pitEntry
198#ifdef WITH_TESTS
199 virtual void
200 dispatchToStrategy(shared_ptr<pit::Entry> pitEntry, function<void(fw::Strategy*)> trigger);
201#else
202 template<class Function>
203 void
204 dispatchToStrategy(shared_ptr<pit::Entry> pitEntry, Function trigger);
205#endif
Junxiao Shid3c792f2014-01-30 00:46:13 -0700206
207private:
Junxiao Shib289cc12014-03-15 12:19:05 -0700208 ForwarderCounters m_counters;
209
Junxiao Shia4f2be82014-03-02 22:56:41 -0700210 FaceTable m_faceTable;
HangZhangad4afd12014-03-01 11:03:08 +0800211
Junxiao Shibb5105f2014-03-03 12:06:45 -0700212 // tables
213 NameTree m_nameTree;
214 Fib m_fib;
215 Pit m_pit;
216 Cs m_cs;
217 Measurements m_measurements;
218 StrategyChoice m_strategyChoice;
Junxiao Shia110f262014-10-12 12:35:20 -0700219 DeadNonceList m_deadNonceList;
Alexander Afanasyev9bcf39e2015-01-08 21:41:48 -0800220 shared_ptr<NullFace> m_csFace;
Junxiao Shibb5105f2014-03-03 12:06:45 -0700221
Spyridon Mastorakisde1f7732014-12-05 22:43:34 -0800222 ns3::Ptr<ns3::ndn::ContentStore> m_csFromNdnSim;
223
Junxiao Shif3c07812014-03-11 21:48:49 -0700224 static const Name LOCALHOST_NAME;
Junxiao Shic041ca32014-02-25 20:01:15 -0700225
Junxiao Shid3c792f2014-01-30 00:46:13 -0700226 // allow Strategy (base class) to enter pipelines
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700227 friend class fw::Strategy;
Alexander Afanasyev33b72772014-01-26 23:22:58 -0800228};
229
Junxiao Shib289cc12014-03-15 12:19:05 -0700230inline const ForwarderCounters&
231Forwarder::getCounters() const
232{
233 return m_counters;
234}
235
Junxiao Shia4f2be82014-03-02 22:56:41 -0700236inline FaceTable&
237Forwarder::getFaceTable()
238{
239 return m_faceTable;
240}
241
242inline shared_ptr<Face>
243Forwarder::getFace(FaceId id) const
244{
245 return m_faceTable.get(id);
246}
247
248inline void
249Forwarder::addFace(shared_ptr<Face> face)
250{
251 m_faceTable.add(face);
252}
253
254inline void
Junxiao Shia4f2be82014-03-02 22:56:41 -0700255Forwarder::onInterest(Face& face, const Interest& interest)
256{
257 this->onIncomingInterest(face, interest);
258}
259
260inline void
261Forwarder::onData(Face& face, const Data& data)
262{
263 this->onIncomingData(face, data);
264}
265
Junxiao Shiea48d8b2014-03-16 13:53:47 -0700266inline NameTree&
267Forwarder::getNameTree()
268{
269 return m_nameTree;
270}
271
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700272inline Fib&
273Forwarder::getFib()
274{
275 return m_fib;
276}
277
278inline Pit&
279Forwarder::getPit()
280{
281 return m_pit;
282}
283
284inline Cs&
285Forwarder::getCs()
286{
287 return m_cs;
288}
289
Junxiao Shidbe71732014-02-21 22:23:28 -0700290inline Measurements&
291Forwarder::getMeasurements()
292{
293 return m_measurements;
294}
295
Junxiao Shibb5105f2014-03-03 12:06:45 -0700296inline StrategyChoice&
297Forwarder::getStrategyChoice()
298{
299 return m_strategyChoice;
300}
Junxiao Shia4f2be82014-03-02 22:56:41 -0700301
Junxiao Shia110f262014-10-12 12:35:20 -0700302inline DeadNonceList&
303Forwarder::getDeadNonceList()
304{
305 return m_deadNonceList;
306}
307
Spyridon Mastorakisde1f7732014-12-05 22:43:34 -0800308inline void
309Forwarder::setCsFromNdnSim(ns3::Ptr<ns3::ndn::ContentStore> cs)
310{
311 m_csFromNdnSim = cs;
312}
313
Junxiao Shif3c07812014-03-11 21:48:49 -0700314#ifdef WITH_TESTS
315inline void
316Forwarder::dispatchToStrategy(shared_ptr<pit::Entry> pitEntry, function<void(fw::Strategy*)> trigger)
317#else
318template<class Function>
319inline void
320Forwarder::dispatchToStrategy(shared_ptr<pit::Entry> pitEntry, Function trigger)
321#endif
322{
323 fw::Strategy& strategy = m_strategyChoice.findEffectiveStrategy(*pitEntry);
324 trigger(&strategy);
325}
326
Junxiao Shid3c792f2014-01-30 00:46:13 -0700327} // namespace nfd
Alexander Afanasyev33b72772014-01-26 23:22:58 -0800328
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700329#endif // NFD_DAEMON_FW_FORWARDER_HPP