blob: 83081b59f98ddf7fc406b66e7ae78cf9b7f400c7 [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 Mastorakis5ea33222016-12-07 14:33:53 -080032#include "ndn-net-device-transport.hpp"
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -080033
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -080034#include "../helper/ndn-stack-helper.hpp"
Spyridon Mastorakisde1f7732014-12-05 22:43:34 -080035#include "cs/ndn-content-store.hpp"
Alexander Afanasyev52e9aa92011-11-15 20:23:20 -080036
Alexander Afanasyevdde1e812015-01-06 14:26:09 -080037#include <boost/property_tree/info_parser.hpp>
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070038
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -080039#include "ns3/ndnSIM/NFD/daemon/fw/forwarder.hpp"
Alexander Afanasyeva91aab42016-09-08 15:47:38 -070040#include "ns3/ndnSIM/NFD/daemon/face/internal-face.hpp"
41#include "ns3/ndnSIM/NFD/daemon/face/internal-transport.hpp"
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -080042#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"
Alexander Afanasyeva91aab42016-09-08 15:47:38 -070045#include "ns3/ndnSIM/NFD/daemon/mgmt/forwarder-status-manager.hpp"
46// #include "ns3/ndnSIM/NFD/daemon/mgmt/general-config-section.hpp"
47#include "ns3/ndnSIM/NFD/daemon/mgmt/tables-config-section.hpp"
Spyridon Mastorakisb0b22412016-12-07 14:33:46 -080048#include "ns3/ndnSIM/NFD/daemon/mgmt/command-authenticator.hpp"
Alexander Afanasyeva91aab42016-09-08 15:47:38 -070049
Spyridon Mastorakis8fde22b2015-08-19 23:12:44 -070050#include "ns3/ndnSIM/NFD/rib/rib-manager.hpp"
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -080051
52#include "ns3/ndnSIM/NFD/daemon/face/null-face.hpp"
Alexander Afanasyeva91aab42016-09-08 15:47:38 -070053#include "ns3/ndnSIM/NFD/daemon/face/internal-face.hpp"
54
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -080055#include "ns3/ndnSIM/NFD/core/config-file.hpp"
Alexander Afanasyeva91aab42016-09-08 15:47:38 -070056
57#include <ndn-cxx/mgmt/dispatcher.hpp>
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -080058
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080059NS_LOG_COMPONENT_DEFINE("ndn.L3Protocol");
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070060
61namespace ns3 {
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070062namespace ndn {
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070063
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070064const uint16_t L3Protocol::ETHERNET_FRAME_TYPE = 0x7777;
Alexander Afanasyev016a5d82013-07-15 10:41:29 -070065const uint16_t L3Protocol::IP_STACK_PORT = 9695;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070066
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080067NS_OBJECT_ENSURE_REGISTERED(L3Protocol);
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070068
Alexander Afanasyev06dba7c2013-02-21 11:36:26 -080069TypeId
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080070L3Protocol::GetTypeId(void)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070071{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080072 static TypeId tid =
73 TypeId("ns3::ndn::L3Protocol")
74 .SetGroupName("ndn")
75 .SetParent<Object>()
76 .AddConstructor<L3Protocol>()
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -080077
78 .AddTraceSource("OutInterests", "OutInterests",
Alexander Afanasyevd6453cd2015-08-20 21:45:36 -070079 MakeTraceSourceAccessor(&L3Protocol::m_outInterests),
80 "ns3::ndn::L3Protocol::InterestTraceCallback")
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -080081 .AddTraceSource("InInterests", "InInterests",
Alexander Afanasyevd6453cd2015-08-20 21:45:36 -070082 MakeTraceSourceAccessor(&L3Protocol::m_inInterests),
83 "ns3::ndn::L3Protocol::InterestTraceCallback")
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -080084
85 ////////////////////////////////////////////////////////////////////
86
Alexander Afanasyevd6453cd2015-08-20 21:45:36 -070087 .AddTraceSource("OutData", "OutData", MakeTraceSourceAccessor(&L3Protocol::m_outData),
88 "ns3::ndn::L3Protocol::DataTraceCallback")
89 .AddTraceSource("InData", "InData", MakeTraceSourceAccessor(&L3Protocol::m_inData),
90 "ns3::ndn::L3Protocol::DataTraceCallback")
Alexander Afanasyevdc6fae82015-01-08 21:44:15 -080091
92 ////////////////////////////////////////////////////////////////////
93
Spyridon Mastorakis5ea33222016-12-07 14:33:53 -080094 .AddTraceSource("OutNack", "OutNack", MakeTraceSourceAccessor(&L3Protocol::m_outNack),
95 "ns3::ndn::L3Protocol::NackTraceCallback")
96 .AddTraceSource("InNack", "InNack", MakeTraceSourceAccessor(&L3Protocol::m_inNack),
97 "ns3::ndn::L3Protocol::NackTraceCallback")
98
99 ////////////////////////////////////////////////////////////////////
100
Alexander Afanasyevdc6fae82015-01-08 21:44:15 -0800101 .AddTraceSource("SatisfiedInterests", "SatisfiedInterests",
Alexander Afanasyevd6453cd2015-08-20 21:45:36 -0700102 MakeTraceSourceAccessor(&L3Protocol::m_satisfiedInterests),
103 "ns3::ndn::L3Protocol::SatisfiedInterestsCallback")
Alexander Afanasyevdc6fae82015-01-08 21:44:15 -0800104 .AddTraceSource("TimedOutInterests", "TimedOutInterests",
Alexander Afanasyevd6453cd2015-08-20 21:45:36 -0700105 MakeTraceSourceAccessor(&L3Protocol::m_timedOutInterests),
106 "ns3::ndn::L3Protocol::TimedOutInterestsCallback")
Alexander Afanasyevdc6fae82015-01-08 21:44:15 -0800107 ;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700108 return tid;
109}
110
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800111class L3Protocol::Impl {
112private:
Alexander Afanasyevdde1e812015-01-06 14:26:09 -0800113 Impl()
114 {
115 // Do not modify initial config file. Use helpers to set specific NFD parameters
116 std::string initialConfig =
117 "general\n"
118 "{\n"
119 "}\n"
120 "\n"
121 "tables\n"
122 "{\n"
123 " cs_max_packets 100\n"
124 "\n"
125 " strategy_choice\n"
126 " {\n"
127 " / /localhost/nfd/strategy/best-route\n"
Alexander Afanasyevc3c7f042015-08-21 11:38:00 -0700128 " /localhost /localhost/nfd/strategy/multicast\n"
Alexander Afanasyevdde1e812015-01-06 14:26:09 -0800129 " /localhost/nfd /localhost/nfd/strategy/best-route\n"
Alexander Afanasyevc3c7f042015-08-21 11:38:00 -0700130 " /ndn/multicast /localhost/nfd/strategy/multicast\n"
Alexander Afanasyevdde1e812015-01-06 14:26:09 -0800131 " }\n"
132 "}\n"
133 "\n"
134 // "face_system\n"
135 // "{\n"
136 // "}\n"
137 "\n"
138 "authorizations\n"
139 "{\n"
140 " authorize\n"
141 " {\n"
142 " certfile any\n"
143 " privileges\n"
144 " {\n"
145 " faces\n"
146 " fib\n"
147 " strategy-choice\n"
148 " }\n"
149 " }\n"
150 "}\n"
151 "\n"
152 "rib\n"
153 "{\n"
154 " localhost_security\n"
155 " {\n"
156 " trust-anchor\n"
157 " {\n"
158 " type any\n"
159 " }\n"
160 " }\n"
161 "}\n"
162 "\n";
163
164 std::istringstream input(initialConfig);
165 boost::property_tree::read_info(input, m_config);
166 }
167
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800168 friend class L3Protocol;
169
Alexander Afanasyeva91aab42016-09-08 15:47:38 -0700170 std::shared_ptr<nfd::Forwarder> m_forwarder;
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800171
Alexander Afanasyeva91aab42016-09-08 15:47:38 -0700172 std::shared_ptr<nfd::Face> m_internalFace;
173 std::shared_ptr<::ndn::Face> m_internalClientFace;
Spyridon Mastorakisb0b22412016-12-07 14:33:46 -0800174 std::shared_ptr<nfd::CommandAuthenticator> m_authenticator;
Alexander Afanasyeva91aab42016-09-08 15:47:38 -0700175
176 std::shared_ptr<nfd::Face> m_internalRibFace;
177 std::shared_ptr<::ndn::Face> m_internalRibClientFace;
178
179 std::unique_ptr<::ndn::mgmt::Dispatcher> m_dispatcher;
Spyridon Mastorakisb0b22412016-12-07 14:33:46 -0800180 std::unique_ptr<::ndn::mgmt::Dispatcher> m_dispatcherRib;
Alexander Afanasyeva91aab42016-09-08 15:47:38 -0700181 std::shared_ptr<nfd::FibManager> m_fibManager;
182 std::shared_ptr<nfd::FaceManager> m_faceManager;
183 std::shared_ptr<nfd::StrategyChoiceManager> m_strategyChoiceManager;
184 std::shared_ptr<nfd::ForwarderStatusManager> m_forwarderStatusManager;
185 std::shared_ptr<nfd::rib::RibManager> m_ribManager;
Spyridon Mastorakisde1f7732014-12-05 22:43:34 -0800186
Spyridon Mastorakisf6d32852017-09-27 20:28:52 -0700187 std::shared_ptr<nfd::face::FaceSystem> m_faceSystem;
188
Alexander Afanasyevdde1e812015-01-06 14:26:09 -0800189 nfd::ConfigSection m_config;
190
Spyridon Mastorakisde1f7732014-12-05 22:43:34 -0800191 Ptr<ContentStore> m_csFromNdnSim;
spirosmastorakis34eed982016-11-02 15:20:50 -0700192 PolicyCreationCallback m_policy;
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800193};
194
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800195L3Protocol::L3Protocol()
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800196 : m_impl(new Impl())
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700197{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800198 NS_LOG_FUNCTION(this);
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700199}
200
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800201L3Protocol::~L3Protocol()
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700202{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800203 NS_LOG_FUNCTION(this);
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700204}
205
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800206void
Alexander Afanasyevdde1e812015-01-06 14:26:09 -0800207L3Protocol::initialize()
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800208{
209 m_impl->m_forwarder = make_shared<nfd::Forwarder>();
210
Alexander Afanasyevdde1e812015-01-06 14:26:09 -0800211 initializeManagement();
spirosmastorakis0df15ba2015-11-14 08:46:24 -0800212
Alexander Afanasyeva91aab42016-09-08 15:47:38 -0700213 nfd::FaceTable& faceTable = m_impl->m_forwarder->getFaceTable();
214 faceTable.addReserved(nfd::face::makeNullFace(), nfd::face::FACEID_NULL);
Alexander Afanasyeva91aab42016-09-08 15:47:38 -0700215
spirosmastorakis0df15ba2015-11-14 08:46:24 -0800216 if (!this->getConfig().get<bool>("ndnSIM.disable_rib_manager", false)) {
217 Simulator::ScheduleWithContext(m_node->GetId(), Seconds(0), &L3Protocol::initializeRibManager, this);
218 }
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800219
Alexander Afanasyevdc6fae82015-01-08 21:44:15 -0800220 m_impl->m_forwarder->beforeSatisfyInterest.connect(std::ref(m_satisfiedInterests));
221 m_impl->m_forwarder->beforeExpirePendingInterest.connect(std::ref(m_timedOutInterests));
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800222}
223
Alexander Afanasyevdde1e812015-01-06 14:26:09 -0800224class IgnoreSections
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800225{
Alexander Afanasyevdde1e812015-01-06 14:26:09 -0800226public:
227 IgnoreSections(const std::vector<std::string>& ignored)
228 : m_ignored(ignored)
229 {
230 }
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800231
Alexander Afanasyevdde1e812015-01-06 14:26:09 -0800232 void
233 operator()(const std::string& filename, const std::string& sectionName,
234 const nfd::ConfigSection& section, bool isDryRun)
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800235
Alexander Afanasyevdde1e812015-01-06 14:26:09 -0800236 {
237 if (std::find(m_ignored.begin(), m_ignored.end(), sectionName) == m_ignored.end()) {
238 nfd::ConfigFile::throwErrorOnUnknownSection(filename, sectionName, section, isDryRun);
239 }
240 }
241private:
242 std::vector<std::string> m_ignored;
243};
244
245void
Alexander Afanasyevca3c67e2016-09-08 15:48:23 -0700246L3Protocol::injectInterest(const Interest& interest)
247{
248 m_impl->m_internalFace->sendInterest(interest);
249}
250
251void
spirosmastorakis34eed982016-11-02 15:20:50 -0700252L3Protocol::setCsReplacementPolicy(const PolicyCreationCallback& policy)
253{
254 m_impl->m_policy = policy;
255}
256
257void
Alexander Afanasyevdde1e812015-01-06 14:26:09 -0800258L3Protocol::initializeManagement()
259{
Alexander Afanasyevdde1e812015-01-06 14:26:09 -0800260 auto& forwarder = m_impl->m_forwarder;
261 using namespace nfd;
262
Spyridon Mastorakisf6d32852017-09-27 20:28:52 -0700263 m_impl->m_faceSystem = make_shared<nfd::face::FaceSystem>(forwarder->getFaceTable(), nullptr);
264
Alexander Afanasyeva91aab42016-09-08 15:47:38 -0700265 std::tie(m_impl->m_internalFace, m_impl->m_internalClientFace) = face::makeInternalFace(StackHelper::getKeyChain());
266 forwarder->getFaceTable().addReserved(m_impl->m_internalFace, face::FACEID_INTERNAL_FACE);
267 m_impl->m_dispatcher.reset(new ::ndn::mgmt::Dispatcher(*m_impl->m_internalClientFace, StackHelper::getKeyChain()));
Alexander Afanasyevdde1e812015-01-06 14:26:09 -0800268
Spyridon Mastorakisb0b22412016-12-07 14:33:46 -0800269 m_impl->m_authenticator = CommandAuthenticator::create();
Alexander Afanasyevdde1e812015-01-06 14:26:09 -0800270
Alexander Afanasyeva91aab42016-09-08 15:47:38 -0700271 m_impl->m_fibManager.reset(new FibManager(forwarder->getFib(),
Spyridon Mastorakisb0b22412016-12-07 14:33:46 -0800272 forwarder->getFaceTable(),
Alexander Afanasyeva91aab42016-09-08 15:47:38 -0700273 *m_impl->m_dispatcher,
Spyridon Mastorakisb0b22412016-12-07 14:33:46 -0800274 *m_impl->m_authenticator));
Alexander Afanasyeva91aab42016-09-08 15:47:38 -0700275
276 // Cannot be disabled for now
Spyridon Mastorakisf6d32852017-09-27 20:28:52 -0700277 // if (!this->getConfig().get<bool>("ndnSIM.disable_face_manager", false))
278
279 m_impl->m_faceManager.reset(new FaceManager(*m_impl->m_faceSystem,
Alexander Afanasyeva91aab42016-09-08 15:47:38 -0700280 *m_impl->m_dispatcher,
Spyridon Mastorakisb0b22412016-12-07 14:33:46 -0800281 *m_impl->m_authenticator));
Alexander Afanasyeva91aab42016-09-08 15:47:38 -0700282 // }
283 // else {
284 // this->getConfig().get_child("authorizations").get_child("authorize").get_child("privileges").erase("faces");
285 // }
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800286
spirosmastorakis0df15ba2015-11-14 08:46:24 -0800287 if (!this->getConfig().get<bool>("ndnSIM.disable_strategy_choice_manager", false)) {
Alexander Afanasyeva91aab42016-09-08 15:47:38 -0700288 m_impl->m_strategyChoiceManager.reset(new StrategyChoiceManager(forwarder->getStrategyChoice(),
289 *m_impl->m_dispatcher,
Spyridon Mastorakisb0b22412016-12-07 14:33:46 -0800290 *m_impl->m_authenticator));
spirosmastorakis0df15ba2015-11-14 08:46:24 -0800291 }
292 else {
293 this->getConfig().get_child("authorizations").get_child("authorize").get_child("privileges").erase("strategy-choice");
294 }
295
Alexander Afanasyeva91aab42016-09-08 15:47:38 -0700296 if (!this->getConfig().get<bool>("ndnSIM.disable_forwarder_status_manager", false)) {
297 m_impl->m_forwarderStatusManager.reset(new ForwarderStatusManager(*forwarder, *m_impl->m_dispatcher));
spirosmastorakis0df15ba2015-11-14 08:46:24 -0800298 }
299
Alexander Afanasyeva91aab42016-09-08 15:47:38 -0700300 ConfigFile config(&ConfigFile::ignoreUnknownSection);
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800301
spirosmastorakis34eed982016-11-02 15:20:50 -0700302 // if we use NFD's CS, we have to specify a replacement policy
303 m_impl->m_csFromNdnSim = GetObject<ContentStore>();
304 if (m_impl->m_csFromNdnSim == nullptr) {
305 forwarder->getCs().setPolicy(m_impl->m_policy());
306 }
307
Spyridon Mastorakisb0b22412016-12-07 14:33:46 -0800308 TablesConfigSection tablesConfig(*forwarder);
Alexander Afanasyevdde1e812015-01-06 14:26:09 -0800309 tablesConfig.setConfigFile(config);
310
Spyridon Mastorakisb0b22412016-12-07 14:33:46 -0800311 m_impl->m_authenticator->setConfigFile(config);
Alexander Afanasyevdde1e812015-01-06 14:26:09 -0800312
Alexander Afanasyeva91aab42016-09-08 15:47:38 -0700313 // if (!this->getConfig().get<bool>("ndnSIM.disable_face_manager", false)) {
Alexander Afanasyevdde1e812015-01-06 14:26:09 -0800314 m_impl->m_faceManager->setConfigFile(config);
Alexander Afanasyeva91aab42016-09-08 15:47:38 -0700315 // }
Alexander Afanasyevdde1e812015-01-06 14:26:09 -0800316
317 // apply config
318 config.parse(m_impl->m_config, false, "ndnSIM.conf");
319
Spyridon Mastorakisb0b22412016-12-07 14:33:46 -0800320 tablesConfig.ensureConfigured();
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800321
322 // add FIB entry for NFD Management Protocol
Alexander Afanasyeva91aab42016-09-08 15:47:38 -0700323 Name topPrefix("/localhost/nfd");
324 auto entry = forwarder->getFib().insert(topPrefix).first;
Spyridon Mastorakisb0b22412016-12-07 14:33:46 -0800325 entry->addNextHop(*(m_impl->m_internalFace), 0);
Alexander Afanasyeva91aab42016-09-08 15:47:38 -0700326 m_impl->m_dispatcher->addTopPrefix(topPrefix, false);
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800327}
328
Spyridon Mastorakis8fde22b2015-08-19 23:12:44 -0700329void
330L3Protocol::initializeRibManager()
331{
332 using namespace nfd;
333
Alexander Afanasyeva91aab42016-09-08 15:47:38 -0700334 std::tie(m_impl->m_internalRibFace, m_impl->m_internalRibClientFace) = face::makeInternalFace(StackHelper::getKeyChain());
335 m_impl->m_forwarder->getFaceTable().add(m_impl->m_internalRibFace);
Spyridon Mastorakisb0b22412016-12-07 14:33:46 -0800336
337 m_impl->m_dispatcherRib.reset(new ::ndn::mgmt::Dispatcher(*m_impl->m_internalRibClientFace, StackHelper::getKeyChain()));
338
339 m_impl->m_ribManager = make_shared<rib::RibManager>(*(m_impl->m_dispatcherRib), *(m_impl->m_internalRibClientFace),
Spyridon Mastorakis8fde22b2015-08-19 23:12:44 -0700340 StackHelper::getKeyChain());
341
342 ConfigFile config([] (const std::string& filename, const std::string& sectionName,
343 const ConfigSection& section, bool isDryRun) {
344 // Ignore "log" and sections belonging to NFD,
345 // but raise an error if we're missing a handler for a "rib" section.
346 if (sectionName != "rib" || sectionName == "log") {
347 // do nothing
348 }
349 else {
Alexander Afanasyeva91aab42016-09-08 15:47:38 -0700350 // missing RIB section
Spyridon Mastorakis8fde22b2015-08-19 23:12:44 -0700351 ConfigFile::throwErrorOnUnknownSection(filename, sectionName, section, isDryRun);
352 }
353 });
354
355 m_impl->m_ribManager->setConfigFile(config);
356
357 // apply config
358 config.parse(m_impl->m_config, false, "ndnSIM.conf");
359
360 m_impl->m_ribManager->registerWithNfd();
Spyridon Mastorakis8fde22b2015-08-19 23:12:44 -0700361}
362
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800363shared_ptr<nfd::Forwarder>
364L3Protocol::getForwarder()
365{
366 return m_impl->m_forwarder;
367}
368
369shared_ptr<nfd::FibManager>
370L3Protocol::getFibManager()
371{
372 return m_impl->m_fibManager;
373}
374
375shared_ptr<nfd::StrategyChoiceManager>
376L3Protocol::getStrategyChoiceManager()
377{
378 return m_impl->m_strategyChoiceManager;
379}
380
Alexander Afanasyevdde1e812015-01-06 14:26:09 -0800381nfd::ConfigSection&
382L3Protocol::getConfig()
383{
384 return m_impl->m_config;
385}
386
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700387/*
388 * This method is called by AddAgregate and completes the aggregation
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700389 * by setting the node in the ndn stack
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700390 */
391void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800392L3Protocol::NotifyNewAggregate()
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700393{
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800394 if (m_node == nullptr) {
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800395 m_node = GetObject<Node>();
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800396 if (m_node != nullptr) {
Spyridon Mastorakis8fde22b2015-08-19 23:12:44 -0700397 initialize();
398
Spyridon Mastorakisde1f7732014-12-05 22:43:34 -0800399 NS_ASSERT(m_impl->m_forwarder != nullptr);
400 m_impl->m_csFromNdnSim = GetObject<ContentStore>();
401 if (m_impl->m_csFromNdnSim != nullptr) {
402 m_impl->m_forwarder->setCsFromNdnSim(m_impl->m_csFromNdnSim);
403 }
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700404 }
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800405 }
Alexander Afanasyevd9fecdd2012-06-08 16:22:24 -0700406
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800407 Object::NotifyNewAggregate();
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700408}
409
Alexander Afanasyev06dba7c2013-02-21 11:36:26 -0800410void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800411L3Protocol::DoDispose(void)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700412{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800413 NS_LOG_FUNCTION(this);
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700414
Alexander Afanasyeva91aab42016-09-08 15:47:38 -0700415 // MUST HAPPEN BEFORE Simulator IS DESTROYED
416 m_impl.reset();
417
418 nfd::scheduler::getGlobalScheduler().cancelAllEvents();
419
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700420 m_node = 0;
Alexander Afanasyevd02a5d62011-11-21 11:01:51 -0800421
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800422 Object::DoDispose();
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700423}
424
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800425nfd::FaceId
426L3Protocol::addFace(shared_ptr<Face> face)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700427{
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800428 NS_LOG_FUNCTION(this << face.get());
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700429
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800430 m_impl->m_forwarder->addFace(face);
431
Alexander Afanasyeva91aab42016-09-08 15:47:38 -0700432 std::weak_ptr<Face> weakFace = face;
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800433
Alexander Afanasyeva91aab42016-09-08 15:47:38 -0700434 // // Connect Signals to TraceSource
435 face->afterReceiveInterest.connect([this, weakFace](const Interest& interest) {
436 shared_ptr<Face> face = weakFace.lock();
437 if (face != nullptr) {
438 this->m_inInterests(interest, *face);
439 }
440 });
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800441
Alexander Afanasyeva91aab42016-09-08 15:47:38 -0700442 face->afterReceiveData.connect([this, weakFace](const Data& data) {
443 shared_ptr<Face> face = weakFace.lock();
444 if (face != nullptr) {
445 this->m_inData(data, *face);
446 }
447 });
Spyridon Mastorakis5ea33222016-12-07 14:33:53 -0800448
449 face->afterReceiveNack.connect([this, weakFace](const lp::Nack& nack) {
450 shared_ptr<Face> face = weakFace.lock();
451 if (face != nullptr) {
452 this->m_inNack(nack, *face);
453 }
454 });
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800455
Alexander Afanasyevac541b12016-09-12 19:45:53 -0700456 auto tracingLink = face->getLinkService();
457 NS_LOG_LOGIC("Adding trace sources for afterSendInterest and afterSendData");
458 tracingLink->afterSendInterest.connect([this, weakFace](const Interest& interest) {
459 shared_ptr<Face> face = weakFace.lock();
460 if (face != nullptr) {
461 this->m_outInterests(interest, *face);
462 }
463 });
Alexander Afanasyeva91aab42016-09-08 15:47:38 -0700464
Alexander Afanasyevac541b12016-09-12 19:45:53 -0700465 tracingLink->afterSendData.connect([this, weakFace](const Data& data) {
466 shared_ptr<Face> face = weakFace.lock();
467 if (face != nullptr) {
468 this->m_outData(data, *face);
469 }
470 });
Alexander Afanasyeva91aab42016-09-08 15:47:38 -0700471
Spyridon Mastorakis5ea33222016-12-07 14:33:53 -0800472 tracingLink->afterSendNack.connect([this, weakFace](const lp::Nack& nack) {
473 shared_ptr<Face> face = weakFace.lock();
474 if (face != nullptr) {
475 this->m_outNack(nack, *face);
476 }
477 });
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800478
479 return face->getId();
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700480}
481
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800482shared_ptr<Face>
483L3Protocol::getFaceById(nfd::FaceId id) const
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700484{
Spyridon Mastorakisb0b22412016-12-07 14:33:46 -0800485 return m_impl->m_forwarder->getFaceTable().get(id)->shared_from_this();
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700486}
487
Spyridon Mastorakise4f0d3c2014-10-29 13:20:03 -0700488shared_ptr<Face>
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800489L3Protocol::getFaceByNetDevice(Ptr<NetDevice> netDevice) const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700490{
Spyridon Mastorakisb0b22412016-12-07 14:33:46 -0800491 for (auto& i : m_impl->m_forwarder->getFaceTable()) {
Spyridon Mastorakis5ea33222016-12-07 14:33:53 -0800492 auto transport = dynamic_cast<NetDeviceTransport*>(i.getTransport());
493 if (transport == nullptr)
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800494 continue;
Alexander Afanasyevaebf5cf2012-08-28 17:32:17 -0700495
Spyridon Mastorakis5ea33222016-12-07 14:33:53 -0800496 if (transport->GetNetDevice() == netDevice)
Spyridon Mastorakisb0b22412016-12-07 14:33:46 -0800497 return i.shared_from_this();
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800498 }
Spyridon Mastorakise4f0d3c2014-10-29 13:20:03 -0700499 return nullptr;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700500}
501
Alexander Afanasyev6b2a3c02015-01-03 02:04:23 -0800502Ptr<L3Protocol>
503L3Protocol::getL3Protocol(Ptr<Object> node)
504{
505 Ptr<L3Protocol> retval = node->GetObject<L3Protocol>();
506 NS_ASSERT_MSG(retval != nullptr, "L3Protocol is not aggregated on this object");
507 return retval;
508}
509
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800510} // namespace ndn
511} // namespace ns3