blob: dd9f4dc8b3e554f84a206fde470aad746afc1cf2 [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 Afanasyevff8c5d62012-04-25 15:14:51 -070031#include "ns3/random-variable.h"
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070032
Spyridon Mastorakise4f0d3c2014-10-29 13:20:03 -070033#include "ndn-face.hpp"
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -080034
Alexander Afanasyev0c395372014-12-20 15:54:02 -080035#include "ndn-net-device-face.hpp"
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -080036#include "../helper/ndn-stack-helper.hpp"
Spyridon Mastorakisde1f7732014-12-05 22:43:34 -080037#include "cs/ndn-content-store.hpp"
Alexander Afanasyev52e9aa92011-11-15 20:23:20 -080038
Alexander Afanasyevdde1e812015-01-06 14:26:09 -080039#include <boost/property_tree/info_parser.hpp>
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070040
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -080041#include "ns3/ndnSIM/NFD/daemon/fw/forwarder.hpp"
42#include "ns3/ndnSIM/NFD/daemon/mgmt/internal-face.hpp"
43#include "ns3/ndnSIM/NFD/daemon/mgmt/fib-manager.hpp"
44#include "ns3/ndnSIM/NFD/daemon/mgmt/face-manager.hpp"
45#include "ns3/ndnSIM/NFD/daemon/mgmt/strategy-choice-manager.hpp"
46#include "ns3/ndnSIM/NFD/daemon/mgmt/status-server.hpp"
47
48#include "ns3/ndnSIM/NFD/daemon/face/null-face.hpp"
49#include "ns3/ndnSIM/NFD/core/config-file.hpp"
50#include "ns3/ndnSIM/NFD/daemon/mgmt/general-config-section.hpp"
51#include "ns3/ndnSIM/NFD/daemon/mgmt/tables-config-section.hpp"
52
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080053NS_LOG_COMPONENT_DEFINE("ndn.L3Protocol");
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070054
55namespace ns3 {
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070056namespace ndn {
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070057
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070058const uint16_t L3Protocol::ETHERNET_FRAME_TYPE = 0x7777;
Alexander Afanasyev016a5d82013-07-15 10:41:29 -070059const uint16_t L3Protocol::IP_STACK_PORT = 9695;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070060
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080061NS_OBJECT_ENSURE_REGISTERED(L3Protocol);
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070062
Alexander Afanasyev06dba7c2013-02-21 11:36:26 -080063TypeId
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080064L3Protocol::GetTypeId(void)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070065{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080066 static TypeId tid =
67 TypeId("ns3::ndn::L3Protocol")
68 .SetGroupName("ndn")
69 .SetParent<Object>()
70 .AddConstructor<L3Protocol>()
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -080071
72 .AddTraceSource("OutInterests", "OutInterests",
73 MakeTraceSourceAccessor(&L3Protocol::m_outInterests))
74 .AddTraceSource("InInterests", "InInterests",
75 MakeTraceSourceAccessor(&L3Protocol::m_inInterests))
76
77 ////////////////////////////////////////////////////////////////////
78
79 .AddTraceSource("OutData", "OutData", MakeTraceSourceAccessor(&L3Protocol::m_outData))
Alexander Afanasyevdc6fae82015-01-08 21:44:15 -080080 .AddTraceSource("InData", "InData", MakeTraceSourceAccessor(&L3Protocol::m_inData))
81
82 ////////////////////////////////////////////////////////////////////
83
84 .AddTraceSource("SatisfiedInterests", "SatisfiedInterests",
85 MakeTraceSourceAccessor(&L3Protocol::m_satisfiedInterests))
86 .AddTraceSource("TimedOutInterests", "TimedOutInterests",
87 MakeTraceSourceAccessor(&L3Protocol::m_timedOutInterests))
88 ;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070089 return tid;
90}
91
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -080092class L3Protocol::Impl {
93private:
Alexander Afanasyevdde1e812015-01-06 14:26:09 -080094 Impl()
95 {
96 // Do not modify initial config file. Use helpers to set specific NFD parameters
97 std::string initialConfig =
98 "general\n"
99 "{\n"
100 "}\n"
101 "\n"
102 "tables\n"
103 "{\n"
104 " cs_max_packets 100\n"
105 "\n"
106 " strategy_choice\n"
107 " {\n"
108 " / /localhost/nfd/strategy/best-route\n"
109 " /localhost /localhost/nfd/strategy/broadcast\n"
110 " /localhost/nfd /localhost/nfd/strategy/best-route\n"
111 " /ndn/broadcast /localhost/nfd/strategy/broadcast\n"
112 " }\n"
113 "}\n"
114 "\n"
115 // "face_system\n"
116 // "{\n"
117 // "}\n"
118 "\n"
119 "authorizations\n"
120 "{\n"
121 " authorize\n"
122 " {\n"
123 " certfile any\n"
124 " privileges\n"
125 " {\n"
126 " faces\n"
127 " fib\n"
128 " strategy-choice\n"
129 " }\n"
130 " }\n"
131 "}\n"
132 "\n"
133 "rib\n"
134 "{\n"
135 " localhost_security\n"
136 " {\n"
137 " trust-anchor\n"
138 " {\n"
139 " type any\n"
140 " }\n"
141 " }\n"
142 "}\n"
143 "\n";
144
145 std::istringstream input(initialConfig);
146 boost::property_tree::read_info(input, m_config);
147 }
148
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800149 friend class L3Protocol;
150
151 shared_ptr<nfd::Forwarder> m_forwarder;
152
153 shared_ptr<nfd::InternalFace> m_internalFace;
154 shared_ptr<nfd::FibManager> m_fibManager;
155 shared_ptr<nfd::FaceManager> m_faceManager;
156 shared_ptr<nfd::StrategyChoiceManager> m_strategyChoiceManager;
157 shared_ptr<nfd::StatusServer> m_statusServer;
Spyridon Mastorakisde1f7732014-12-05 22:43:34 -0800158
Alexander Afanasyevdde1e812015-01-06 14:26:09 -0800159 nfd::ConfigSection m_config;
160
Spyridon Mastorakisde1f7732014-12-05 22:43:34 -0800161 Ptr<ContentStore> m_csFromNdnSim;
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800162};
163
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800164L3Protocol::L3Protocol()
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800165 : m_impl(new Impl())
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700166{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800167 NS_LOG_FUNCTION(this);
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700168}
169
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800170L3Protocol::~L3Protocol()
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
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800175void
Alexander Afanasyevdde1e812015-01-06 14:26:09 -0800176L3Protocol::initialize()
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800177{
178 m_impl->m_forwarder = make_shared<nfd::Forwarder>();
179
Alexander Afanasyevdde1e812015-01-06 14:26:09 -0800180 initializeManagement();
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800181
182 m_impl->m_forwarder->getFaceTable().addReserved(make_shared<nfd::NullFace>(), nfd::FACEID_NULL);
Alexander Afanasyevdc6fae82015-01-08 21:44:15 -0800183
184 m_impl->m_forwarder->beforeSatisfyInterest.connect(std::ref(m_satisfiedInterests));
185 m_impl->m_forwarder->beforeExpirePendingInterest.connect(std::ref(m_timedOutInterests));
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800186}
187
Alexander Afanasyevdde1e812015-01-06 14:26:09 -0800188class IgnoreSections
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800189{
Alexander Afanasyevdde1e812015-01-06 14:26:09 -0800190public:
191 IgnoreSections(const std::vector<std::string>& ignored)
192 : m_ignored(ignored)
193 {
194 }
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800195
Alexander Afanasyevdde1e812015-01-06 14:26:09 -0800196 void
197 operator()(const std::string& filename, const std::string& sectionName,
198 const nfd::ConfigSection& section, bool isDryRun)
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800199
Alexander Afanasyevdde1e812015-01-06 14:26:09 -0800200 {
201 if (std::find(m_ignored.begin(), m_ignored.end(), sectionName) == m_ignored.end()) {
202 nfd::ConfigFile::throwErrorOnUnknownSection(filename, sectionName, section, isDryRun);
203 }
204 }
205private:
206 std::vector<std::string> m_ignored;
207};
208
209void
210L3Protocol::initializeManagement()
211{
212 auto keyChain = std::ref(StackHelper::getKeyChain());
213 auto& forwarder = m_impl->m_forwarder;
214 using namespace nfd;
215
216 m_impl->m_internalFace = make_shared<InternalFace>();
217
218 m_impl->m_fibManager = make_shared<FibManager>(std::ref(forwarder->getFib()),
219 bind(&Forwarder::getFace, forwarder.get(), _1),
220 m_impl->m_internalFace, keyChain);
221
222 m_impl->m_faceManager = make_shared<FaceManager>(std::ref(forwarder->getFaceTable()),
223 m_impl->m_internalFace,
224 keyChain);
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800225
226 m_impl->m_strategyChoiceManager =
Alexander Afanasyevdde1e812015-01-06 14:26:09 -0800227 make_shared<StrategyChoiceManager>(std::ref(forwarder->getStrategyChoice()),
228 m_impl->m_internalFace,
229 keyChain);
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800230
Alexander Afanasyevdde1e812015-01-06 14:26:09 -0800231 m_impl->m_statusServer = make_shared<StatusServer>(m_impl->m_internalFace,
232 ref(*forwarder),
233 keyChain);
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800234
Alexander Afanasyevdde1e812015-01-06 14:26:09 -0800235 ConfigFile config((IgnoreSections({"general", "log", "rib"})));
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800236
Alexander Afanasyevdde1e812015-01-06 14:26:09 -0800237 TablesConfigSection tablesConfig(forwarder->getCs(),
238 forwarder->getPit(),
239 forwarder->getFib(),
240 forwarder->getStrategyChoice(),
241 forwarder->getMeasurements());
242 tablesConfig.setConfigFile(config);
243
244 m_impl->m_internalFace->getValidator().setConfigFile(config);
245
246 forwarder->getFaceTable().addReserved(m_impl->m_internalFace, FACEID_INTERNAL_FACE);
247
248 m_impl->m_faceManager->setConfigFile(config);
249
250 // apply config
251 config.parse(m_impl->m_config, false, "ndnSIM.conf");
252
253 tablesConfig.ensureTablesAreConfigured();
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800254
255 // add FIB entry for NFD Management Protocol
Alexander Afanasyevdde1e812015-01-06 14:26:09 -0800256 shared_ptr<fib::Entry> entry = forwarder->getFib().insert("/localhost/nfd").first;
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800257 entry->addNextHop(m_impl->m_internalFace, 0);
258}
259
260shared_ptr<nfd::Forwarder>
261L3Protocol::getForwarder()
262{
263 return m_impl->m_forwarder;
264}
265
266shared_ptr<nfd::FibManager>
267L3Protocol::getFibManager()
268{
269 return m_impl->m_fibManager;
270}
271
272shared_ptr<nfd::StrategyChoiceManager>
273L3Protocol::getStrategyChoiceManager()
274{
275 return m_impl->m_strategyChoiceManager;
276}
277
Alexander Afanasyevdde1e812015-01-06 14:26:09 -0800278nfd::ConfigSection&
279L3Protocol::getConfig()
280{
281 return m_impl->m_config;
282}
283
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700284/*
285 * This method is called by AddAgregate and completes the aggregation
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700286 * by setting the node in the ndn stack
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700287 */
288void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800289L3Protocol::NotifyNewAggregate()
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700290{
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800291 if (m_node == nullptr) {
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800292 m_node = GetObject<Node>();
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800293 if (m_node != nullptr) {
Spyridon Mastorakisde1f7732014-12-05 22:43:34 -0800294 NS_ASSERT(m_impl->m_forwarder != nullptr);
295 m_impl->m_csFromNdnSim = GetObject<ContentStore>();
296 if (m_impl->m_csFromNdnSim != nullptr) {
297 m_impl->m_forwarder->setCsFromNdnSim(m_impl->m_csFromNdnSim);
298 }
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700299 }
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800300 }
Alexander Afanasyevd9fecdd2012-06-08 16:22:24 -0700301
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800302 Object::NotifyNewAggregate();
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700303}
304
Alexander Afanasyev06dba7c2013-02-21 11:36:26 -0800305void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800306L3Protocol::DoDispose(void)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700307{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800308 NS_LOG_FUNCTION(this);
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700309
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700310 m_node = 0;
Alexander Afanasyevd02a5d62011-11-21 11:01:51 -0800311
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800312 Object::DoDispose();
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700313}
314
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800315nfd::FaceId
316L3Protocol::addFace(shared_ptr<Face> face)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700317{
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800318 NS_LOG_FUNCTION(this << face.get());
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700319
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800320 m_impl->m_forwarder->addFace(face);
321
322 // Connect Signals to TraceSource
323 face->onReceiveInterest +=
324 [this, face](const Interest& interest) { this->m_inInterests(interest, *face); };
325
326 face->onSendInterest +=
327 [this, face](const Interest& interest) { this->m_outInterests(interest, *face); };
328
329 face->onReceiveData += [this, face](const Data& data) { this->m_inData(data, *face); };
330
331 face->onSendData += [this, face](const Data& data) { this->m_outData(data, *face); };
332
333 return face->getId();
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700334}
335
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800336// void
337// L3Protocol::removeFace(shared_ptr<Face> face)
338// {
339// NS_LOG_FUNCTION(this << std::cref(*face));
340// }
341
342shared_ptr<Face>
343L3Protocol::getFaceById(nfd::FaceId id) const
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700344{
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800345 return m_impl->m_forwarder->getFaceTable().get(id);
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700346}
347
Spyridon Mastorakise4f0d3c2014-10-29 13:20:03 -0700348shared_ptr<Face>
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800349L3Protocol::getFaceByNetDevice(Ptr<NetDevice> netDevice) const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700350{
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800351 for (const auto& i : m_impl->m_forwarder->getFaceTable()) {
352 shared_ptr<NetDeviceFace> netDeviceFace = std::dynamic_pointer_cast<NetDeviceFace>(i);
353 if (netDeviceFace == nullptr)
354 continue;
Alexander Afanasyevaebf5cf2012-08-28 17:32:17 -0700355
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800356 if (netDeviceFace->GetNetDevice() == netDevice)
357 return i;
358 }
Spyridon Mastorakise4f0d3c2014-10-29 13:20:03 -0700359 return nullptr;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700360}
361
Alexander Afanasyev6b2a3c02015-01-03 02:04:23 -0800362Ptr<L3Protocol>
363L3Protocol::getL3Protocol(Ptr<Object> node)
364{
365 Ptr<L3Protocol> retval = node->GetObject<L3Protocol>();
366 NS_ASSERT_MSG(retval != nullptr, "L3Protocol is not aggregated on this object");
367 return retval;
368}
369
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800370} // namespace ndn
371} // namespace ns3