blob: f383477ad4c1604592b07c4fab2e953e4eba09d6 [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"
Alexander Afanasyev52e9aa92011-11-15 20:23:20 -080039
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070040#include <boost/foreach.hpp>
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070041
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -080042#include "ns3/ndnSIM/NFD/daemon/fw/forwarder.hpp"
43#include "ns3/ndnSIM/NFD/daemon/mgmt/internal-face.hpp"
44#include "ns3/ndnSIM/NFD/daemon/mgmt/fib-manager.hpp"
45#include "ns3/ndnSIM/NFD/daemon/mgmt/face-manager.hpp"
46#include "ns3/ndnSIM/NFD/daemon/mgmt/strategy-choice-manager.hpp"
47#include "ns3/ndnSIM/NFD/daemon/mgmt/status-server.hpp"
48
49#include "ns3/ndnSIM/NFD/daemon/face/null-face.hpp"
50#include "ns3/ndnSIM/NFD/core/config-file.hpp"
51#include "ns3/ndnSIM/NFD/daemon/mgmt/general-config-section.hpp"
52#include "ns3/ndnSIM/NFD/daemon/mgmt/tables-config-section.hpp"
53
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080054NS_LOG_COMPONENT_DEFINE("ndn.L3Protocol");
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070055
56namespace ns3 {
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070057namespace ndn {
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070058
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070059const uint16_t L3Protocol::ETHERNET_FRAME_TYPE = 0x7777;
Alexander Afanasyev016a5d82013-07-15 10:41:29 -070060const uint16_t L3Protocol::IP_STACK_PORT = 9695;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070061
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080062NS_OBJECT_ENSURE_REGISTERED(L3Protocol);
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070063
Alexander Afanasyev06dba7c2013-02-21 11:36:26 -080064TypeId
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080065L3Protocol::GetTypeId(void)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070066{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080067 static TypeId tid =
68 TypeId("ns3::ndn::L3Protocol")
69 .SetGroupName("ndn")
70 .SetParent<Object>()
71 .AddConstructor<L3Protocol>()
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -080072
73 .AddTraceSource("OutInterests", "OutInterests",
74 MakeTraceSourceAccessor(&L3Protocol::m_outInterests))
75 .AddTraceSource("InInterests", "InInterests",
76 MakeTraceSourceAccessor(&L3Protocol::m_inInterests))
77
78 ////////////////////////////////////////////////////////////////////
79
80 .AddTraceSource("OutData", "OutData", MakeTraceSourceAccessor(&L3Protocol::m_outData))
81 .AddTraceSource("InData", "InData", MakeTraceSourceAccessor(&L3Protocol::m_inData));
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070082 return tid;
83}
84
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -080085class L3Protocol::Impl {
86private:
87 friend class L3Protocol;
88
89 shared_ptr<nfd::Forwarder> m_forwarder;
90
91 shared_ptr<nfd::InternalFace> m_internalFace;
92 shared_ptr<nfd::FibManager> m_fibManager;
93 shared_ptr<nfd::FaceManager> m_faceManager;
94 shared_ptr<nfd::StrategyChoiceManager> m_strategyChoiceManager;
95 shared_ptr<nfd::StatusServer> m_statusServer;
96};
97
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080098L3Protocol::L3Protocol()
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -080099 : m_impl(new Impl())
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700100{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800101 NS_LOG_FUNCTION(this);
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700102}
103
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800104L3Protocol::~L3Protocol()
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700105{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800106 NS_LOG_FUNCTION(this);
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700107}
108
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800109void
110L3Protocol::initialize()
111{
112 m_impl->m_forwarder = make_shared<nfd::Forwarder>();
113
114 initializeManagement();
115
116 m_impl->m_forwarder->getFaceTable().addReserved(make_shared<nfd::NullFace>(), nfd::FACEID_NULL);
117 m_impl->m_forwarder->getFaceTable().addReserved(make_shared<nfd::NullFace>(
118 FaceUri("contentstore://")),
119 nfd::FACEID_CONTENT_STORE);
120}
121
122void
123L3Protocol::initializeManagement()
124{
125 m_impl->m_internalFace = make_shared<nfd::InternalFace>();
126
127 m_impl->m_fibManager =
128 make_shared<nfd::FibManager>(ref(m_impl->m_forwarder->getFib()),
129 bind(&nfd::Forwarder::getFace, m_impl->m_forwarder.get(), _1),
130 m_impl->m_internalFace, std::ref(StackHelper::getKeyChain()));
131
132 m_impl->m_faceManager =
133 make_shared<nfd::FaceManager>(ref(m_impl->m_forwarder->getFaceTable()), m_impl->m_internalFace,
134 std::ref(StackHelper::getKeyChain()));
135
136 m_impl->m_strategyChoiceManager =
137 make_shared<nfd::StrategyChoiceManager>(ref(m_impl->m_forwarder->getStrategyChoice()),
138 m_impl->m_internalFace,
139 std::ref(StackHelper::getKeyChain()));
140
141 m_impl->m_statusServer =
142 make_shared<nfd::StatusServer>(m_impl->m_internalFace, ref(*m_impl->m_forwarder),
143 std::ref(StackHelper::getKeyChain()));
144
145 nfd::TablesConfigSection tablesConfig(m_impl->m_forwarder->getCs(),
146 m_impl->m_forwarder->getPit(),
147 m_impl->m_forwarder->getFib(),
148 m_impl->m_forwarder->getStrategyChoice(),
149 m_impl->m_forwarder->getMeasurements());
150
151 m_impl->m_forwarder->getFaceTable().addReserved(m_impl->m_internalFace,
152 nfd::FACEID_INTERNAL_FACE);
153
154 // add FIB entry for NFD Management Protocol
155 shared_ptr<::nfd::fib::Entry> entry =
156 m_impl->m_forwarder->getFib().insert("/localhost/nfd").first;
157 entry->addNextHop(m_impl->m_internalFace, 0);
158}
159
160shared_ptr<nfd::Forwarder>
161L3Protocol::getForwarder()
162{
163 return m_impl->m_forwarder;
164}
165
166shared_ptr<nfd::FibManager>
167L3Protocol::getFibManager()
168{
169 return m_impl->m_fibManager;
170}
171
172shared_ptr<nfd::StrategyChoiceManager>
173L3Protocol::getStrategyChoiceManager()
174{
175 return m_impl->m_strategyChoiceManager;
176}
177
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700178/*
179 * This method is called by AddAgregate and completes the aggregation
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700180 * by setting the node in the ndn stack
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700181 */
182void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800183L3Protocol::NotifyNewAggregate()
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700184{
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800185 if (m_node == nullptr) {
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800186 m_node = GetObject<Node>();
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800187 if (m_node != nullptr) {
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700188 }
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800189 }
Alexander Afanasyevd9fecdd2012-06-08 16:22:24 -0700190
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800191 Object::NotifyNewAggregate();
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700192}
193
Alexander Afanasyev06dba7c2013-02-21 11:36:26 -0800194void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800195L3Protocol::DoDispose(void)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700196{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800197 NS_LOG_FUNCTION(this);
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700198
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700199 m_node = 0;
Alexander Afanasyevd02a5d62011-11-21 11:01:51 -0800200
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800201 Object::DoDispose();
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700202}
203
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800204nfd::FaceId
205L3Protocol::addFace(shared_ptr<Face> face)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700206{
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800207 NS_LOG_FUNCTION(this << face.get());
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700208
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800209 m_impl->m_forwarder->addFace(face);
210
211 // Connect Signals to TraceSource
212 face->onReceiveInterest +=
213 [this, face](const Interest& interest) { this->m_inInterests(interest, *face); };
214
215 face->onSendInterest +=
216 [this, face](const Interest& interest) { this->m_outInterests(interest, *face); };
217
218 face->onReceiveData += [this, face](const Data& data) { this->m_inData(data, *face); };
219
220 face->onSendData += [this, face](const Data& data) { this->m_outData(data, *face); };
221
222 return face->getId();
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700223}
224
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800225// void
226// L3Protocol::removeFace(shared_ptr<Face> face)
227// {
228// NS_LOG_FUNCTION(this << std::cref(*face));
229// }
230
231shared_ptr<Face>
232L3Protocol::getFaceById(nfd::FaceId id) const
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700233{
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800234 return m_impl->m_forwarder->getFaceTable().get(id);
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700235}
236
Spyridon Mastorakise4f0d3c2014-10-29 13:20:03 -0700237shared_ptr<Face>
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800238L3Protocol::getFaceByNetDevice(Ptr<NetDevice> netDevice) const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700239{
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800240 for (const auto& i : m_impl->m_forwarder->getFaceTable()) {
241 shared_ptr<NetDeviceFace> netDeviceFace = std::dynamic_pointer_cast<NetDeviceFace>(i);
242 if (netDeviceFace == nullptr)
243 continue;
Alexander Afanasyevaebf5cf2012-08-28 17:32:17 -0700244
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800245 if (netDeviceFace->GetNetDevice() == netDevice)
246 return i;
247 }
Spyridon Mastorakise4f0d3c2014-10-29 13:20:03 -0700248 return nullptr;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700249}
250
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800251} // namespace ndn
252} // namespace ns3