blob: ad9c0d7f61e746f36d2d59c1332f3b63de9dd402 [file] [log] [blame]
Alexander Afanasyev60a7b622014-12-20 17:04:07 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2011-2015 Regents of the University of California.
Alexander Afanasyevab1d5602011-08-17 19:17:18 -07004 *
Alexander Afanasyev60a7b622014-12-20 17:04:07 -08005 * This file is part of ndnSIM. See AUTHORS for complete list of ndnSIM authors and
6 * contributors.
Alexander Afanasyevab1d5602011-08-17 19:17:18 -07007 *
Alexander Afanasyev60a7b622014-12-20 17:04:07 -08008 * ndnSIM is free software: you can redistribute it and/or modify it under the terms
9 * of the GNU General Public License as published by the Free Software Foundation,
10 * either version 3 of the License, or (at your option) any later version.
Alexander Afanasyevab1d5602011-08-17 19:17:18 -070011 *
Alexander Afanasyev60a7b622014-12-20 17:04:07 -080012 * ndnSIM is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
13 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
Alexander Afanasyevab1d5602011-08-17 19:17:18 -070015 *
Alexander Afanasyev60a7b622014-12-20 17:04:07 -080016 * You should have received a copy of the GNU General Public License along with
17 * ndnSIM, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
18 **/
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070019
Alexander Afanasyev0c395372014-12-20 15:54:02 -080020#include "ndn-l3-protocol.hpp"
Alexander Afanasyev45b92d42011-08-14 23:11:38 -070021
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070022#include "ns3/packet.h"
Alexander Afanasyev45b92d42011-08-14 23:11:38 -070023#include "ns3/node.h"
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070024#include "ns3/log.h"
25#include "ns3/callback.h"
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070026#include "ns3/uinteger.h"
27#include "ns3/trace-source-accessor.h"
28#include "ns3/object-vector.h"
Alexander Afanasyevcbe92ae2011-12-16 13:06:18 -080029#include "ns3/pointer.h"
Alexander Afanasyev4975f732011-12-20 17:52:19 -080030#include "ns3/simulator.h"
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070031
Spyridon Mastorakise4f0d3c2014-10-29 13:20:03 -070032#include "ndn-face.hpp"
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -080033
Alexander Afanasyev0c395372014-12-20 15:54:02 -080034#include "ndn-net-device-face.hpp"
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -080035#include "../helper/ndn-stack-helper.hpp"
Spyridon Mastorakisde1f7732014-12-05 22:43:34 -080036#include "cs/ndn-content-store.hpp"
Alexander Afanasyev52e9aa92011-11-15 20:23:20 -080037
Alexander Afanasyevdde1e812015-01-06 14:26:09 -080038#include <boost/property_tree/info_parser.hpp>
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070039
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -080040#include "ns3/ndnSIM/NFD/daemon/fw/forwarder.hpp"
41#include "ns3/ndnSIM/NFD/daemon/mgmt/internal-face.hpp"
42#include "ns3/ndnSIM/NFD/daemon/mgmt/fib-manager.hpp"
43#include "ns3/ndnSIM/NFD/daemon/mgmt/face-manager.hpp"
44#include "ns3/ndnSIM/NFD/daemon/mgmt/strategy-choice-manager.hpp"
45#include "ns3/ndnSIM/NFD/daemon/mgmt/status-server.hpp"
46
47#include "ns3/ndnSIM/NFD/daemon/face/null-face.hpp"
48#include "ns3/ndnSIM/NFD/core/config-file.hpp"
49#include "ns3/ndnSIM/NFD/daemon/mgmt/general-config-section.hpp"
50#include "ns3/ndnSIM/NFD/daemon/mgmt/tables-config-section.hpp"
51
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080052NS_LOG_COMPONENT_DEFINE("ndn.L3Protocol");
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070053
54namespace ns3 {
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070055namespace ndn {
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070056
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070057const uint16_t L3Protocol::ETHERNET_FRAME_TYPE = 0x7777;
Alexander Afanasyev016a5d82013-07-15 10:41:29 -070058const uint16_t L3Protocol::IP_STACK_PORT = 9695;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070059
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080060NS_OBJECT_ENSURE_REGISTERED(L3Protocol);
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070061
Alexander Afanasyev06dba7c2013-02-21 11:36:26 -080062TypeId
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080063L3Protocol::GetTypeId(void)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070064{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080065 static TypeId tid =
66 TypeId("ns3::ndn::L3Protocol")
67 .SetGroupName("ndn")
68 .SetParent<Object>()
69 .AddConstructor<L3Protocol>()
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -080070
71 .AddTraceSource("OutInterests", "OutInterests",
Alexander Afanasyevd6453cd2015-08-20 21:45:36 -070072 MakeTraceSourceAccessor(&L3Protocol::m_outInterests),
73 "ns3::ndn::L3Protocol::InterestTraceCallback")
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -080074 .AddTraceSource("InInterests", "InInterests",
Alexander Afanasyevd6453cd2015-08-20 21:45:36 -070075 MakeTraceSourceAccessor(&L3Protocol::m_inInterests),
76 "ns3::ndn::L3Protocol::InterestTraceCallback")
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -080077
78 ////////////////////////////////////////////////////////////////////
79
Alexander Afanasyevd6453cd2015-08-20 21:45:36 -070080 .AddTraceSource("OutData", "OutData", MakeTraceSourceAccessor(&L3Protocol::m_outData),
81 "ns3::ndn::L3Protocol::DataTraceCallback")
82 .AddTraceSource("InData", "InData", MakeTraceSourceAccessor(&L3Protocol::m_inData),
83 "ns3::ndn::L3Protocol::DataTraceCallback")
Alexander Afanasyevdc6fae82015-01-08 21:44:15 -080084
85 ////////////////////////////////////////////////////////////////////
86
87 .AddTraceSource("SatisfiedInterests", "SatisfiedInterests",
Alexander Afanasyevd6453cd2015-08-20 21:45:36 -070088 MakeTraceSourceAccessor(&L3Protocol::m_satisfiedInterests),
89 "ns3::ndn::L3Protocol::SatisfiedInterestsCallback")
Alexander Afanasyevdc6fae82015-01-08 21:44:15 -080090 .AddTraceSource("TimedOutInterests", "TimedOutInterests",
Alexander Afanasyevd6453cd2015-08-20 21:45:36 -070091 MakeTraceSourceAccessor(&L3Protocol::m_timedOutInterests),
92 "ns3::ndn::L3Protocol::TimedOutInterestsCallback")
Alexander Afanasyevdc6fae82015-01-08 21:44:15 -080093 ;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070094 return tid;
95}
96
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -080097class L3Protocol::Impl {
98private:
Alexander Afanasyevdde1e812015-01-06 14:26:09 -080099 Impl()
100 {
101 // Do not modify initial config file. Use helpers to set specific NFD parameters
102 std::string initialConfig =
103 "general\n"
104 "{\n"
105 "}\n"
106 "\n"
107 "tables\n"
108 "{\n"
109 " cs_max_packets 100\n"
110 "\n"
111 " strategy_choice\n"
112 " {\n"
113 " / /localhost/nfd/strategy/best-route\n"
Alexander Afanasyevc3c7f042015-08-21 11:38:00 -0700114 " /localhost /localhost/nfd/strategy/multicast\n"
Alexander Afanasyevdde1e812015-01-06 14:26:09 -0800115 " /localhost/nfd /localhost/nfd/strategy/best-route\n"
Alexander Afanasyevc3c7f042015-08-21 11:38:00 -0700116 " /ndn/multicast /localhost/nfd/strategy/multicast\n"
Alexander Afanasyevdde1e812015-01-06 14:26:09 -0800117 " }\n"
118 "}\n"
119 "\n"
120 // "face_system\n"
121 // "{\n"
122 // "}\n"
123 "\n"
124 "authorizations\n"
125 "{\n"
126 " authorize\n"
127 " {\n"
128 " certfile any\n"
129 " privileges\n"
130 " {\n"
131 " faces\n"
132 " fib\n"
133 " strategy-choice\n"
134 " }\n"
135 " }\n"
136 "}\n"
137 "\n"
138 "rib\n"
139 "{\n"
140 " localhost_security\n"
141 " {\n"
142 " trust-anchor\n"
143 " {\n"
144 " type any\n"
145 " }\n"
146 " }\n"
147 "}\n"
148 "\n";
149
150 std::istringstream input(initialConfig);
151 boost::property_tree::read_info(input, m_config);
152 }
153
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800154 friend class L3Protocol;
155
156 shared_ptr<nfd::Forwarder> m_forwarder;
157
158 shared_ptr<nfd::InternalFace> m_internalFace;
159 shared_ptr<nfd::FibManager> m_fibManager;
160 shared_ptr<nfd::FaceManager> m_faceManager;
161 shared_ptr<nfd::StrategyChoiceManager> m_strategyChoiceManager;
162 shared_ptr<nfd::StatusServer> m_statusServer;
Spyridon Mastorakisde1f7732014-12-05 22:43:34 -0800163
Alexander Afanasyevdde1e812015-01-06 14:26:09 -0800164 nfd::ConfigSection m_config;
165
Spyridon Mastorakisde1f7732014-12-05 22:43:34 -0800166 Ptr<ContentStore> m_csFromNdnSim;
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800167};
168
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800169L3Protocol::L3Protocol()
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800170 : m_impl(new Impl())
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700171{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800172 NS_LOG_FUNCTION(this);
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700173}
174
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800175L3Protocol::~L3Protocol()
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700176{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800177 NS_LOG_FUNCTION(this);
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700178}
179
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800180void
Alexander Afanasyevdde1e812015-01-06 14:26:09 -0800181L3Protocol::initialize()
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800182{
183 m_impl->m_forwarder = make_shared<nfd::Forwarder>();
184
Alexander Afanasyevdde1e812015-01-06 14:26:09 -0800185 initializeManagement();
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800186
187 m_impl->m_forwarder->getFaceTable().addReserved(make_shared<nfd::NullFace>(), nfd::FACEID_NULL);
Alexander Afanasyevdc6fae82015-01-08 21:44:15 -0800188
189 m_impl->m_forwarder->beforeSatisfyInterest.connect(std::ref(m_satisfiedInterests));
190 m_impl->m_forwarder->beforeExpirePendingInterest.connect(std::ref(m_timedOutInterests));
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800191}
192
Alexander Afanasyevdde1e812015-01-06 14:26:09 -0800193class IgnoreSections
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800194{
Alexander Afanasyevdde1e812015-01-06 14:26:09 -0800195public:
196 IgnoreSections(const std::vector<std::string>& ignored)
197 : m_ignored(ignored)
198 {
199 }
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800200
Alexander Afanasyevdde1e812015-01-06 14:26:09 -0800201 void
202 operator()(const std::string& filename, const std::string& sectionName,
203 const nfd::ConfigSection& section, bool isDryRun)
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800204
Alexander Afanasyevdde1e812015-01-06 14:26:09 -0800205 {
206 if (std::find(m_ignored.begin(), m_ignored.end(), sectionName) == m_ignored.end()) {
207 nfd::ConfigFile::throwErrorOnUnknownSection(filename, sectionName, section, isDryRun);
208 }
209 }
210private:
211 std::vector<std::string> m_ignored;
212};
213
214void
215L3Protocol::initializeManagement()
216{
217 auto keyChain = std::ref(StackHelper::getKeyChain());
218 auto& forwarder = m_impl->m_forwarder;
219 using namespace nfd;
220
221 m_impl->m_internalFace = make_shared<InternalFace>();
222
223 m_impl->m_fibManager = make_shared<FibManager>(std::ref(forwarder->getFib()),
224 bind(&Forwarder::getFace, forwarder.get(), _1),
225 m_impl->m_internalFace, keyChain);
226
227 m_impl->m_faceManager = make_shared<FaceManager>(std::ref(forwarder->getFaceTable()),
228 m_impl->m_internalFace,
229 keyChain);
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800230
231 m_impl->m_strategyChoiceManager =
Alexander Afanasyevdde1e812015-01-06 14:26:09 -0800232 make_shared<StrategyChoiceManager>(std::ref(forwarder->getStrategyChoice()),
233 m_impl->m_internalFace,
234 keyChain);
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800235
Alexander Afanasyevdde1e812015-01-06 14:26:09 -0800236 m_impl->m_statusServer = make_shared<StatusServer>(m_impl->m_internalFace,
237 ref(*forwarder),
238 keyChain);
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800239
Alexander Afanasyevdde1e812015-01-06 14:26:09 -0800240 ConfigFile config((IgnoreSections({"general", "log", "rib"})));
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800241
Alexander Afanasyevdde1e812015-01-06 14:26:09 -0800242 TablesConfigSection tablesConfig(forwarder->getCs(),
243 forwarder->getPit(),
244 forwarder->getFib(),
245 forwarder->getStrategyChoice(),
246 forwarder->getMeasurements());
247 tablesConfig.setConfigFile(config);
248
249 m_impl->m_internalFace->getValidator().setConfigFile(config);
250
251 forwarder->getFaceTable().addReserved(m_impl->m_internalFace, FACEID_INTERNAL_FACE);
252
253 m_impl->m_faceManager->setConfigFile(config);
254
255 // apply config
256 config.parse(m_impl->m_config, false, "ndnSIM.conf");
257
258 tablesConfig.ensureTablesAreConfigured();
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800259
260 // add FIB entry for NFD Management Protocol
Alexander Afanasyevdde1e812015-01-06 14:26:09 -0800261 shared_ptr<fib::Entry> entry = forwarder->getFib().insert("/localhost/nfd").first;
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800262 entry->addNextHop(m_impl->m_internalFace, 0);
263}
264
265shared_ptr<nfd::Forwarder>
266L3Protocol::getForwarder()
267{
268 return m_impl->m_forwarder;
269}
270
271shared_ptr<nfd::FibManager>
272L3Protocol::getFibManager()
273{
274 return m_impl->m_fibManager;
275}
276
277shared_ptr<nfd::StrategyChoiceManager>
278L3Protocol::getStrategyChoiceManager()
279{
280 return m_impl->m_strategyChoiceManager;
281}
282
Alexander Afanasyevdde1e812015-01-06 14:26:09 -0800283nfd::ConfigSection&
284L3Protocol::getConfig()
285{
286 return m_impl->m_config;
287}
288
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700289/*
290 * This method is called by AddAgregate and completes the aggregation
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700291 * by setting the node in the ndn stack
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700292 */
293void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800294L3Protocol::NotifyNewAggregate()
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700295{
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800296 if (m_node == nullptr) {
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800297 m_node = GetObject<Node>();
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800298 if (m_node != nullptr) {
Spyridon Mastorakisde1f7732014-12-05 22:43:34 -0800299 NS_ASSERT(m_impl->m_forwarder != nullptr);
300 m_impl->m_csFromNdnSim = GetObject<ContentStore>();
301 if (m_impl->m_csFromNdnSim != nullptr) {
302 m_impl->m_forwarder->setCsFromNdnSim(m_impl->m_csFromNdnSim);
303 }
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700304 }
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800305 }
Alexander Afanasyevd9fecdd2012-06-08 16:22:24 -0700306
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800307 Object::NotifyNewAggregate();
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700308}
309
Alexander Afanasyev06dba7c2013-02-21 11:36:26 -0800310void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800311L3Protocol::DoDispose(void)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700312{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800313 NS_LOG_FUNCTION(this);
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700314
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700315 m_node = 0;
Alexander Afanasyevd02a5d62011-11-21 11:01:51 -0800316
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800317 Object::DoDispose();
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700318}
319
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800320nfd::FaceId
321L3Protocol::addFace(shared_ptr<Face> face)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700322{
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800323 NS_LOG_FUNCTION(this << face.get());
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700324
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800325 m_impl->m_forwarder->addFace(face);
326
327 // Connect Signals to TraceSource
Alexander Afanasyevc3c7f042015-08-21 11:38:00 -0700328 face->onReceiveInterest.connect
329 ([this, face](const Interest& interest) { this->m_inInterests(interest, *face); });
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800330
Alexander Afanasyevc3c7f042015-08-21 11:38:00 -0700331 face->onSendInterest.connect
332 ([this, face](const Interest& interest) { this->m_outInterests(interest, *face); });
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800333
Alexander Afanasyevc3c7f042015-08-21 11:38:00 -0700334 face->onReceiveData.connect([this, face](const Data& data) { this->m_inData(data, *face); });
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800335
Alexander Afanasyevc3c7f042015-08-21 11:38:00 -0700336 face->onSendData.connect([this, face](const Data& data) { this->m_outData(data, *face); });
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800337
338 return face->getId();
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700339}
340
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800341// void
342// L3Protocol::removeFace(shared_ptr<Face> face)
343// {
344// NS_LOG_FUNCTION(this << std::cref(*face));
345// }
346
347shared_ptr<Face>
348L3Protocol::getFaceById(nfd::FaceId id) const
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700349{
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800350 return m_impl->m_forwarder->getFaceTable().get(id);
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700351}
352
Spyridon Mastorakise4f0d3c2014-10-29 13:20:03 -0700353shared_ptr<Face>
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800354L3Protocol::getFaceByNetDevice(Ptr<NetDevice> netDevice) const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700355{
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800356 for (const auto& i : m_impl->m_forwarder->getFaceTable()) {
357 shared_ptr<NetDeviceFace> netDeviceFace = std::dynamic_pointer_cast<NetDeviceFace>(i);
358 if (netDeviceFace == nullptr)
359 continue;
Alexander Afanasyevaebf5cf2012-08-28 17:32:17 -0700360
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800361 if (netDeviceFace->GetNetDevice() == netDevice)
362 return i;
363 }
Spyridon Mastorakise4f0d3c2014-10-29 13:20:03 -0700364 return nullptr;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700365}
366
Alexander Afanasyev6b2a3c02015-01-03 02:04:23 -0800367Ptr<L3Protocol>
368L3Protocol::getL3Protocol(Ptr<Object> node)
369{
370 Ptr<L3Protocol> retval = node->GetObject<L3Protocol>();
371 NS_ASSERT_MSG(retval != nullptr, "L3Protocol is not aggregated on this object");
372 return retval;
373}
374
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800375} // namespace ndn
376} // namespace ns3