blob: afcab4323294210b7d480e6624f6b4d188b6f62a [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 Afanasyev56f79ea2011-08-17 23:54:27 -070039#include <boost/foreach.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))
80 .AddTraceSource("InData", "InData", MakeTraceSourceAccessor(&L3Protocol::m_inData));
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070081 return tid;
82}
83
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -080084class L3Protocol::Impl {
85private:
86 friend class L3Protocol;
87
88 shared_ptr<nfd::Forwarder> m_forwarder;
89
90 shared_ptr<nfd::InternalFace> m_internalFace;
91 shared_ptr<nfd::FibManager> m_fibManager;
92 shared_ptr<nfd::FaceManager> m_faceManager;
93 shared_ptr<nfd::StrategyChoiceManager> m_strategyChoiceManager;
94 shared_ptr<nfd::StatusServer> m_statusServer;
Spyridon Mastorakisde1f7732014-12-05 22:43:34 -080095
96 Ptr<ContentStore> m_csFromNdnSim;
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -080097};
98
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080099L3Protocol::L3Protocol()
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800100 : m_impl(new Impl())
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700101{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800102 NS_LOG_FUNCTION(this);
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700103}
104
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800105L3Protocol::~L3Protocol()
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700106{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800107 NS_LOG_FUNCTION(this);
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700108}
109
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800110void
Spyridon Mastorakisde1f7732014-12-05 22:43:34 -0800111L3Protocol::initialize(bool shouldUseNfdCs)
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800112{
113 m_impl->m_forwarder = make_shared<nfd::Forwarder>();
114
Spyridon Mastorakisde1f7732014-12-05 22:43:34 -0800115 initializeManagement(shouldUseNfdCs);
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800116
117 m_impl->m_forwarder->getFaceTable().addReserved(make_shared<nfd::NullFace>(), nfd::FACEID_NULL);
118 m_impl->m_forwarder->getFaceTable().addReserved(make_shared<nfd::NullFace>(
119 FaceUri("contentstore://")),
120 nfd::FACEID_CONTENT_STORE);
121}
122
123void
Spyridon Mastorakisde1f7732014-12-05 22:43:34 -0800124L3Protocol::initializeManagement(bool shouldUseNfdCs)
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800125{
126 m_impl->m_internalFace = make_shared<nfd::InternalFace>();
127
128 m_impl->m_fibManager =
129 make_shared<nfd::FibManager>(ref(m_impl->m_forwarder->getFib()),
130 bind(&nfd::Forwarder::getFace, m_impl->m_forwarder.get(), _1),
131 m_impl->m_internalFace, std::ref(StackHelper::getKeyChain()));
132
133 m_impl->m_faceManager =
134 make_shared<nfd::FaceManager>(ref(m_impl->m_forwarder->getFaceTable()), m_impl->m_internalFace,
135 std::ref(StackHelper::getKeyChain()));
136
137 m_impl->m_strategyChoiceManager =
138 make_shared<nfd::StrategyChoiceManager>(ref(m_impl->m_forwarder->getStrategyChoice()),
139 m_impl->m_internalFace,
140 std::ref(StackHelper::getKeyChain()));
141
142 m_impl->m_statusServer =
143 make_shared<nfd::StatusServer>(m_impl->m_internalFace, ref(*m_impl->m_forwarder),
144 std::ref(StackHelper::getKeyChain()));
145
146 nfd::TablesConfigSection tablesConfig(m_impl->m_forwarder->getCs(),
147 m_impl->m_forwarder->getPit(),
148 m_impl->m_forwarder->getFib(),
149 m_impl->m_forwarder->getStrategyChoice(),
150 m_impl->m_forwarder->getMeasurements());
151
152 m_impl->m_forwarder->getFaceTable().addReserved(m_impl->m_internalFace,
153 nfd::FACEID_INTERNAL_FACE);
154
155 // add FIB entry for NFD Management Protocol
156 shared_ptr<::nfd::fib::Entry> entry =
157 m_impl->m_forwarder->getFib().insert("/localhost/nfd").first;
158 entry->addNextHop(m_impl->m_internalFace, 0);
159}
160
161shared_ptr<nfd::Forwarder>
162L3Protocol::getForwarder()
163{
164 return m_impl->m_forwarder;
165}
166
167shared_ptr<nfd::FibManager>
168L3Protocol::getFibManager()
169{
170 return m_impl->m_fibManager;
171}
172
173shared_ptr<nfd::StrategyChoiceManager>
174L3Protocol::getStrategyChoiceManager()
175{
176 return m_impl->m_strategyChoiceManager;
177}
178
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700179/*
180 * This method is called by AddAgregate and completes the aggregation
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700181 * by setting the node in the ndn stack
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700182 */
183void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800184L3Protocol::NotifyNewAggregate()
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700185{
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800186 if (m_node == nullptr) {
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800187 m_node = GetObject<Node>();
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800188 if (m_node != nullptr) {
Spyridon Mastorakisde1f7732014-12-05 22:43:34 -0800189 NS_ASSERT(m_impl->m_forwarder != nullptr);
190 m_impl->m_csFromNdnSim = GetObject<ContentStore>();
191 if (m_impl->m_csFromNdnSim != nullptr) {
192 m_impl->m_forwarder->setCsFromNdnSim(m_impl->m_csFromNdnSim);
193 }
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700194 }
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800195 }
Alexander Afanasyevd9fecdd2012-06-08 16:22:24 -0700196
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800197 Object::NotifyNewAggregate();
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700198}
199
Alexander Afanasyev06dba7c2013-02-21 11:36:26 -0800200void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800201L3Protocol::DoDispose(void)
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
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700205 m_node = 0;
Alexander Afanasyevd02a5d62011-11-21 11:01:51 -0800206
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800207 Object::DoDispose();
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700208}
209
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800210nfd::FaceId
211L3Protocol::addFace(shared_ptr<Face> face)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700212{
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800213 NS_LOG_FUNCTION(this << face.get());
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700214
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800215 m_impl->m_forwarder->addFace(face);
216
217 // Connect Signals to TraceSource
218 face->onReceiveInterest +=
219 [this, face](const Interest& interest) { this->m_inInterests(interest, *face); };
220
221 face->onSendInterest +=
222 [this, face](const Interest& interest) { this->m_outInterests(interest, *face); };
223
224 face->onReceiveData += [this, face](const Data& data) { this->m_inData(data, *face); };
225
226 face->onSendData += [this, face](const Data& data) { this->m_outData(data, *face); };
227
228 return face->getId();
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700229}
230
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800231// void
232// L3Protocol::removeFace(shared_ptr<Face> face)
233// {
234// NS_LOG_FUNCTION(this << std::cref(*face));
235// }
236
237shared_ptr<Face>
238L3Protocol::getFaceById(nfd::FaceId id) const
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700239{
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800240 return m_impl->m_forwarder->getFaceTable().get(id);
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700241}
242
Spyridon Mastorakise4f0d3c2014-10-29 13:20:03 -0700243shared_ptr<Face>
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800244L3Protocol::getFaceByNetDevice(Ptr<NetDevice> netDevice) const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700245{
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800246 for (const auto& i : m_impl->m_forwarder->getFaceTable()) {
247 shared_ptr<NetDeviceFace> netDeviceFace = std::dynamic_pointer_cast<NetDeviceFace>(i);
248 if (netDeviceFace == nullptr)
249 continue;
Alexander Afanasyevaebf5cf2012-08-28 17:32:17 -0700250
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800251 if (netDeviceFace->GetNetDevice() == netDevice)
252 return i;
253 }
Spyridon Mastorakise4f0d3c2014-10-29 13:20:03 -0700254 return nullptr;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700255}
256
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800257} // namespace ndn
258} // namespace ns3