blob: 7412e2cab6091f5d4720a21a3a95ed4639a1362e [file] [log] [blame]
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:ni -*- */
Alexander Afanasyevab1d5602011-08-17 19:17:18 -07002/*
3 * Copyright (c) 2011 University of California, Los Angeles
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation;
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
Ilya Moiseenko172763c2011-10-28 13:21:53 -070019 * Ilya Moiseenko <iliamo@cs.ucla.edu>
Alexander Afanasyevab1d5602011-08-17 19:17:18 -070020 */
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070021
Alexander Afanasyev0c395372014-12-20 15:54:02 -080022#include "ndn-l3-protocol.hpp"
Alexander Afanasyev45b92d42011-08-14 23:11:38 -070023
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070024#include "ns3/packet.h"
Alexander Afanasyev45b92d42011-08-14 23:11:38 -070025#include "ns3/node.h"
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070026#include "ns3/log.h"
27#include "ns3/callback.h"
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070028#include "ns3/uinteger.h"
29#include "ns3/trace-source-accessor.h"
30#include "ns3/object-vector.h"
Alexander Afanasyevcbe92ae2011-12-16 13:06:18 -080031#include "ns3/pointer.h"
Alexander Afanasyev4975f732011-12-20 17:52:19 -080032#include "ns3/simulator.h"
Alexander Afanasyevff8c5d62012-04-25 15:14:51 -070033#include "ns3/random-variable.h"
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070034
Spyridon Mastorakise4f0d3c2014-10-29 13:20:03 -070035#include "ndn-face.hpp"
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -080036
Alexander Afanasyev0c395372014-12-20 15:54:02 -080037#include "ndn-net-device-face.hpp"
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -080038#include "../helper/ndn-stack-helper.hpp"
Spyridon Mastorakisde1f7732014-12-05 22:43:34 -080039#include "cs/ndn-content-store.hpp"
Alexander Afanasyev52e9aa92011-11-15 20:23:20 -080040
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070041#include <boost/foreach.hpp>
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070042
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -080043#include "ns3/ndnSIM/NFD/daemon/fw/forwarder.hpp"
44#include "ns3/ndnSIM/NFD/daemon/mgmt/internal-face.hpp"
45#include "ns3/ndnSIM/NFD/daemon/mgmt/fib-manager.hpp"
46#include "ns3/ndnSIM/NFD/daemon/mgmt/face-manager.hpp"
47#include "ns3/ndnSIM/NFD/daemon/mgmt/strategy-choice-manager.hpp"
48#include "ns3/ndnSIM/NFD/daemon/mgmt/status-server.hpp"
49
50#include "ns3/ndnSIM/NFD/daemon/face/null-face.hpp"
51#include "ns3/ndnSIM/NFD/core/config-file.hpp"
52#include "ns3/ndnSIM/NFD/daemon/mgmt/general-config-section.hpp"
53#include "ns3/ndnSIM/NFD/daemon/mgmt/tables-config-section.hpp"
54
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080055NS_LOG_COMPONENT_DEFINE("ndn.L3Protocol");
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070056
57namespace ns3 {
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070058namespace ndn {
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070059
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070060const uint16_t L3Protocol::ETHERNET_FRAME_TYPE = 0x7777;
Alexander Afanasyev016a5d82013-07-15 10:41:29 -070061const uint16_t L3Protocol::IP_STACK_PORT = 9695;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070062
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080063NS_OBJECT_ENSURE_REGISTERED(L3Protocol);
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070064
Alexander Afanasyev06dba7c2013-02-21 11:36:26 -080065TypeId
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080066L3Protocol::GetTypeId(void)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070067{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080068 static TypeId tid =
69 TypeId("ns3::ndn::L3Protocol")
70 .SetGroupName("ndn")
71 .SetParent<Object>()
72 .AddConstructor<L3Protocol>()
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -080073
74 .AddTraceSource("OutInterests", "OutInterests",
75 MakeTraceSourceAccessor(&L3Protocol::m_outInterests))
76 .AddTraceSource("InInterests", "InInterests",
77 MakeTraceSourceAccessor(&L3Protocol::m_inInterests))
78
79 ////////////////////////////////////////////////////////////////////
80
81 .AddTraceSource("OutData", "OutData", MakeTraceSourceAccessor(&L3Protocol::m_outData))
82 .AddTraceSource("InData", "InData", MakeTraceSourceAccessor(&L3Protocol::m_inData));
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070083 return tid;
84}
85
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -080086class L3Protocol::Impl {
87private:
88 friend class L3Protocol;
89
90 shared_ptr<nfd::Forwarder> m_forwarder;
91
92 shared_ptr<nfd::InternalFace> m_internalFace;
93 shared_ptr<nfd::FibManager> m_fibManager;
94 shared_ptr<nfd::FaceManager> m_faceManager;
95 shared_ptr<nfd::StrategyChoiceManager> m_strategyChoiceManager;
96 shared_ptr<nfd::StatusServer> m_statusServer;
Spyridon Mastorakisde1f7732014-12-05 22:43:34 -080097
98 Ptr<ContentStore> m_csFromNdnSim;
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -080099};
100
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800101L3Protocol::L3Protocol()
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800102 : m_impl(new Impl())
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700103{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800104 NS_LOG_FUNCTION(this);
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700105}
106
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800107L3Protocol::~L3Protocol()
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700108{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800109 NS_LOG_FUNCTION(this);
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700110}
111
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800112void
Spyridon Mastorakisde1f7732014-12-05 22:43:34 -0800113L3Protocol::initialize(bool shouldUseNfdCs)
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800114{
115 m_impl->m_forwarder = make_shared<nfd::Forwarder>();
116
Spyridon Mastorakisde1f7732014-12-05 22:43:34 -0800117 initializeManagement(shouldUseNfdCs);
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800118
119 m_impl->m_forwarder->getFaceTable().addReserved(make_shared<nfd::NullFace>(), nfd::FACEID_NULL);
120 m_impl->m_forwarder->getFaceTable().addReserved(make_shared<nfd::NullFace>(
121 FaceUri("contentstore://")),
122 nfd::FACEID_CONTENT_STORE);
123}
124
125void
Spyridon Mastorakisde1f7732014-12-05 22:43:34 -0800126L3Protocol::initializeManagement(bool shouldUseNfdCs)
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800127{
128 m_impl->m_internalFace = make_shared<nfd::InternalFace>();
129
130 m_impl->m_fibManager =
131 make_shared<nfd::FibManager>(ref(m_impl->m_forwarder->getFib()),
132 bind(&nfd::Forwarder::getFace, m_impl->m_forwarder.get(), _1),
133 m_impl->m_internalFace, std::ref(StackHelper::getKeyChain()));
134
135 m_impl->m_faceManager =
136 make_shared<nfd::FaceManager>(ref(m_impl->m_forwarder->getFaceTable()), m_impl->m_internalFace,
137 std::ref(StackHelper::getKeyChain()));
138
139 m_impl->m_strategyChoiceManager =
140 make_shared<nfd::StrategyChoiceManager>(ref(m_impl->m_forwarder->getStrategyChoice()),
141 m_impl->m_internalFace,
142 std::ref(StackHelper::getKeyChain()));
143
144 m_impl->m_statusServer =
145 make_shared<nfd::StatusServer>(m_impl->m_internalFace, ref(*m_impl->m_forwarder),
146 std::ref(StackHelper::getKeyChain()));
147
148 nfd::TablesConfigSection tablesConfig(m_impl->m_forwarder->getCs(),
149 m_impl->m_forwarder->getPit(),
150 m_impl->m_forwarder->getFib(),
151 m_impl->m_forwarder->getStrategyChoice(),
152 m_impl->m_forwarder->getMeasurements());
153
154 m_impl->m_forwarder->getFaceTable().addReserved(m_impl->m_internalFace,
155 nfd::FACEID_INTERNAL_FACE);
156
157 // add FIB entry for NFD Management Protocol
158 shared_ptr<::nfd::fib::Entry> entry =
159 m_impl->m_forwarder->getFib().insert("/localhost/nfd").first;
160 entry->addNextHop(m_impl->m_internalFace, 0);
161}
162
163shared_ptr<nfd::Forwarder>
164L3Protocol::getForwarder()
165{
166 return m_impl->m_forwarder;
167}
168
169shared_ptr<nfd::FibManager>
170L3Protocol::getFibManager()
171{
172 return m_impl->m_fibManager;
173}
174
175shared_ptr<nfd::StrategyChoiceManager>
176L3Protocol::getStrategyChoiceManager()
177{
178 return m_impl->m_strategyChoiceManager;
179}
180
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700181/*
182 * This method is called by AddAgregate and completes the aggregation
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700183 * by setting the node in the ndn stack
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700184 */
185void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800186L3Protocol::NotifyNewAggregate()
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700187{
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800188 if (m_node == nullptr) {
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800189 m_node = GetObject<Node>();
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800190 if (m_node != nullptr) {
Spyridon Mastorakisde1f7732014-12-05 22:43:34 -0800191 NS_ASSERT(m_impl->m_forwarder != nullptr);
192 m_impl->m_csFromNdnSim = GetObject<ContentStore>();
193 if (m_impl->m_csFromNdnSim != nullptr) {
194 m_impl->m_forwarder->setCsFromNdnSim(m_impl->m_csFromNdnSim);
195 }
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700196 }
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800197 }
Alexander Afanasyevd9fecdd2012-06-08 16:22:24 -0700198
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800199 Object::NotifyNewAggregate();
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700200}
201
Alexander Afanasyev06dba7c2013-02-21 11:36:26 -0800202void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800203L3Protocol::DoDispose(void)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700204{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800205 NS_LOG_FUNCTION(this);
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700206
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700207 m_node = 0;
Alexander Afanasyevd02a5d62011-11-21 11:01:51 -0800208
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800209 Object::DoDispose();
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700210}
211
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800212nfd::FaceId
213L3Protocol::addFace(shared_ptr<Face> face)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700214{
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800215 NS_LOG_FUNCTION(this << face.get());
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700216
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800217 m_impl->m_forwarder->addFace(face);
218
219 // Connect Signals to TraceSource
220 face->onReceiveInterest +=
221 [this, face](const Interest& interest) { this->m_inInterests(interest, *face); };
222
223 face->onSendInterest +=
224 [this, face](const Interest& interest) { this->m_outInterests(interest, *face); };
225
226 face->onReceiveData += [this, face](const Data& data) { this->m_inData(data, *face); };
227
228 face->onSendData += [this, face](const Data& data) { this->m_outData(data, *face); };
229
230 return face->getId();
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700231}
232
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800233// void
234// L3Protocol::removeFace(shared_ptr<Face> face)
235// {
236// NS_LOG_FUNCTION(this << std::cref(*face));
237// }
238
239shared_ptr<Face>
240L3Protocol::getFaceById(nfd::FaceId id) const
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700241{
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800242 return m_impl->m_forwarder->getFaceTable().get(id);
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700243}
244
Spyridon Mastorakise4f0d3c2014-10-29 13:20:03 -0700245shared_ptr<Face>
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800246L3Protocol::getFaceByNetDevice(Ptr<NetDevice> netDevice) const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700247{
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800248 for (const auto& i : m_impl->m_forwarder->getFaceTable()) {
249 shared_ptr<NetDeviceFace> netDeviceFace = std::dynamic_pointer_cast<NetDeviceFace>(i);
250 if (netDeviceFace == nullptr)
251 continue;
Alexander Afanasyevaebf5cf2012-08-28 17:32:17 -0700252
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800253 if (netDeviceFace->GetNetDevice() == netDevice)
254 return i;
255 }
Spyridon Mastorakise4f0d3c2014-10-29 13:20:03 -0700256 return nullptr;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700257}
258
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800259} // namespace ndn
260} // namespace ns3