blob: 6e45281e47b7e53cb79be46fdbc4ab596135de5a [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 Afanasyev45b92d42011-08-14 23:11:38 -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 Afanasyev45b92d42011-08-14 23:11:38 -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 Afanasyev45b92d42011-08-14 23:11:38 -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 Afanasyev45b92d42011-08-14 23:11:38 -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 Afanasyev45b92d42011-08-14 23:11:38 -070019
Alexander Afanasyev0c395372014-12-20 15:54:02 -080020#include "ndn-stack-helper.hpp"
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -080021
22#include "ns3/log.h"
23#include "ns3/names.h"
Spyridon Mastorakisde1f7732014-12-05 22:43:34 -080024#include "ns3/string.h"
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -080025#include "ns3/point-to-point-net-device.h"
26
27#include "model/ndn-l3-protocol.hpp"
28#include "model/ndn-net-device-face.hpp"
29#include "utils/ndn-time.hpp"
Alexander Afanasyev34e13f32014-12-14 15:13:28 -080030#include "utils/dummy-keychain.hpp"
Spyridon Mastorakisde1f7732014-12-05 22:43:34 -080031#include "model/cs/ndn-content-store.hpp"
Alexander Afanasyev45b92d42011-08-14 23:11:38 -070032
33#include <limits>
34#include <map>
Alexander Afanasyevb7626842012-01-12 13:43:33 -080035#include <boost/lexical_cast.hpp>
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -080036
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080037NS_LOG_COMPONENT_DEFINE("ndn.StackHelper");
Alexander Afanasyev45b92d42011-08-14 23:11:38 -070038
39namespace ns3 {
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070040namespace ndn {
Alexander Afanasyev122f3782013-02-02 00:04:40 -080041
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080042StackHelper::StackHelper()
Spyridon Mastorakis588fd102014-11-20 19:50:02 -080043 : m_needSetDefaultRoutes(false)
Alexander Afanasyevdde1e812015-01-06 14:26:09 -080044 , m_maxCsSize(100)
Alexander Afanasyev45b92d42011-08-14 23:11:38 -070045{
Spyridon Mastorakis86edf6f2014-11-14 19:27:18 -080046 setCustomNdnCxxClocks();
47
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080048 m_ndnFactory.SetTypeId("ns3::ndn::L3Protocol");
Spyridon Mastorakisde1f7732014-12-05 22:43:34 -080049 m_contentStoreFactory.SetTypeId("ns3::ndn::cs::Lru");
Alexander Afanasyev122f3782013-02-02 00:04:40 -080050
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080051 m_netDeviceCallbacks.push_back(
52 std::make_pair(PointToPointNetDevice::GetTypeId(),
53 MakeCallback(&StackHelper::PointToPointNetDeviceCallback, this)));
Alexander Afanasyev122f3782013-02-02 00:04:40 -080054 // default callback will be fired if non of others callbacks fit or did the job
Ilya Moiseenko25f7d4d2011-09-29 18:41:06 -070055}
Alexander Afanasyev122f3782013-02-02 00:04:40 -080056
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080057StackHelper::~StackHelper()
Alexander Afanasyev45b92d42011-08-14 23:11:38 -070058{
Alexander Afanasyev45b92d42011-08-14 23:11:38 -070059}
60
Alexander Afanasyev34e13f32014-12-14 15:13:28 -080061KeyChain&
62StackHelper::getKeyChain()
63{
64 static ::ndn::DummyKeyChain keyChain;
65 return keyChain;
66}
67
Alexander Afanasyev3a4a0b32012-06-28 14:14:22 -070068void
Spyridon Mastorakis86edf6f2014-11-14 19:27:18 -080069StackHelper::setCustomNdnCxxClocks()
70{
71 ::ndn::time::setCustomClocks(make_shared<ns3::ndn::time::CustomSteadyClock>(),
72 make_shared<ns3::ndn::time::CustomSystemClock>());
73}
74
Spyridon Mastorakis588fd102014-11-20 19:50:02 -080075void
76StackHelper::SetDefaultRoutes(bool needSet)
77{
78 NS_LOG_FUNCTION(this << needSet);
79 m_needSetDefaultRoutes = needSet;
80}
81
Spyridon Mastorakisde1f7732014-12-05 22:43:34 -080082void
83StackHelper::SetStackAttributes(const std::string& attr1, const std::string& value1,
84 const std::string& attr2, const std::string& value2,
85 const std::string& attr3, const std::string& value3,
86 const std::string& attr4, const std::string& value4)
87{
88 if (attr1 != "")
89 m_ndnFactory.Set(attr1, StringValue(value1));
90 if (attr2 != "")
91 m_ndnFactory.Set(attr2, StringValue(value2));
92 if (attr3 != "")
93 m_ndnFactory.Set(attr3, StringValue(value3));
94 if (attr4 != "")
95 m_ndnFactory.Set(attr4, StringValue(value4));
96}
97
98void
Alexander Afanasyevdde1e812015-01-06 14:26:09 -080099StackHelper::SetOldContentStore(const std::string& contentStore, const std::string& attr1,
100 const std::string& value1, const std::string& attr2,
101 const std::string& value2, const std::string& attr3,
102 const std::string& value3, const std::string& attr4,
103 const std::string& value4)
Spyridon Mastorakisde1f7732014-12-05 22:43:34 -0800104{
Alexander Afanasyevdde1e812015-01-06 14:26:09 -0800105 m_maxCsSize = 0;
Spyridon Mastorakisde1f7732014-12-05 22:43:34 -0800106
107 m_contentStoreFactory.SetTypeId(contentStore);
108 if (attr1 != "")
109 m_contentStoreFactory.Set(attr1, StringValue(value1));
110 if (attr2 != "")
111 m_contentStoreFactory.Set(attr2, StringValue(value2));
112 if (attr3 != "")
113 m_contentStoreFactory.Set(attr3, StringValue(value3));
114 if (attr4 != "")
115 m_contentStoreFactory.Set(attr4, StringValue(value4));
116}
117
118void
Alexander Afanasyevdde1e812015-01-06 14:26:09 -0800119StackHelper::setCsSize(size_t maxSize)
Spyridon Mastorakisde1f7732014-12-05 22:43:34 -0800120{
Alexander Afanasyevdde1e812015-01-06 14:26:09 -0800121 m_maxCsSize = maxSize;
Spyridon Mastorakisde1f7732014-12-05 22:43:34 -0800122}
123
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700124Ptr<FaceContainer>
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800125StackHelper::Install(const NodeContainer& c) const
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700126{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800127 Ptr<FaceContainer> faces = Create<FaceContainer>();
128 for (NodeContainer::Iterator i = c.Begin(); i != c.End(); ++i) {
129 faces->AddAll(Install(*i));
130 }
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700131 return faces;
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700132}
133
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700134Ptr<FaceContainer>
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800135StackHelper::InstallAll() const
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700136{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800137 return Install(NodeContainer::GetGlobal());
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700138}
139
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700140Ptr<FaceContainer>
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800141StackHelper::Install(Ptr<Node> node) const
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700142{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800143 Ptr<FaceContainer> faces = Create<FaceContainer>();
Alexander Afanasyev122f3782013-02-02 00:04:40 -0800144
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800145 if (node->GetObject<L3Protocol>() != 0) {
146 NS_FATAL_ERROR("StackHelper::Install (): Installing "
147 "a NdnStack to a node with an existing Ndn object");
148 return 0;
149 }
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700150
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800151 Ptr<L3Protocol> ndn = m_ndnFactory.Create<L3Protocol>();
Alexander Afanasyev122f3782013-02-02 00:04:40 -0800152
Alexander Afanasyevdde1e812015-01-06 14:26:09 -0800153 ndn->getConfig().put("tables.cs_max_packets", (m_maxCsSize == 0) ? 1 : m_maxCsSize);
154
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800155 // NFD initialization
Alexander Afanasyevdde1e812015-01-06 14:26:09 -0800156 ndn->initialize();
Spyridon Mastorakisde1f7732014-12-05 22:43:34 -0800157
158 // Create and aggregate content store if NFD's contest store has been disabled
Alexander Afanasyevdde1e812015-01-06 14:26:09 -0800159 if (m_maxCsSize == 0) {
Spyridon Mastorakisde1f7732014-12-05 22:43:34 -0800160 ndn->AggregateObject(m_contentStoreFactory.Create<ContentStore>());
Alexander Afanasyevdde1e812015-01-06 14:26:09 -0800161 }
Spyridon Mastorakisde1f7732014-12-05 22:43:34 -0800162
163 // Aggregate L3Protocol on node (must be after setting ndnSIM CS)
164 node->AggregateObject(ndn);
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800165
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800166 for (uint32_t index = 0; index < node->GetNDevices(); index++) {
167 Ptr<NetDevice> device = node->GetDevice(index);
168 // This check does not make sense: LoopbackNetDevice is installed only if IP stack is installed,
169 // Normally, ndnSIM works without IP stack, so no reason to check
170 // if (DynamicCast<LoopbackNetDevice> (device) != 0)
171 // continue; // don't create face for a LoopbackNetDevice
Alexander Afanasyev11453142011-11-25 16:13:33 -0800172
Spyridon Mastorakise4f0d3c2014-10-29 13:20:03 -0700173 shared_ptr<NetDeviceFace> face;
Alexander Afanasyev122f3782013-02-02 00:04:40 -0800174
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800175 for (const auto& item : m_netDeviceCallbacks) {
176 if (device->GetInstanceTypeId() == item.first ||
177 device->GetInstanceTypeId().IsChildOf(item.first)) {
178 face = item.second(node, ndn, device);
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800179 if (face != 0)
180 break;
181 }
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700182 }
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800183
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800184 if (face == 0) {
185 face = DefaultNetDeviceCallback(node, ndn, device);
186 }
187
Spyridon Mastorakis588fd102014-11-20 19:50:02 -0800188 if (m_needSetDefaultRoutes) {
189 // default route with lowest priority possible
190 FibHelper::AddRoute(node, "/", face, std::numeric_limits<int32_t>::max());
191 }
192
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800193 faces->Add(face);
194 }
Alexander Afanasyev122f3782013-02-02 00:04:40 -0800195
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700196 return faces;
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700197}
198
Alexander Afanasyev122f3782013-02-02 00:04:40 -0800199void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800200StackHelper::AddNetDeviceFaceCreateCallback(TypeId netDeviceType,
201 StackHelper::NetDeviceFaceCreateCallback callback)
Alexander Afanasyev122f3782013-02-02 00:04:40 -0800202{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800203 m_netDeviceCallbacks.push_back(std::make_pair(netDeviceType, callback));
Alexander Afanasyev122f3782013-02-02 00:04:40 -0800204}
205
Alexander Afanasyev2a269f72013-06-06 22:59:33 -0700206void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800207StackHelper::UpdateNetDeviceFaceCreateCallback(TypeId netDeviceType,
208 NetDeviceFaceCreateCallback callback)
Alexander Afanasyev2a269f72013-06-06 22:59:33 -0700209{
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800210 for (auto& i : m_netDeviceCallbacks) {
211 if (i.first == netDeviceType) {
212 i.second = callback;
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800213 return;
Alexander Afanasyev2a269f72013-06-06 22:59:33 -0700214 }
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800215 }
Alexander Afanasyev2a269f72013-06-06 22:59:33 -0700216}
217
218void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800219StackHelper::RemoveNetDeviceFaceCreateCallback(TypeId netDeviceType,
220 NetDeviceFaceCreateCallback callback)
Alexander Afanasyev2a269f72013-06-06 22:59:33 -0700221{
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800222 m_netDeviceCallbacks.remove_if([&] (const std::pair<TypeId, NetDeviceFaceCreateCallback>& i) {
223 return (i.first == netDeviceType);
224 });
Alexander Afanasyev2a269f72013-06-06 22:59:33 -0700225}
Alexander Afanasyev122f3782013-02-02 00:04:40 -0800226
Spyridon Mastorakise4f0d3c2014-10-29 13:20:03 -0700227shared_ptr<NetDeviceFace>
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800228StackHelper::DefaultNetDeviceCallback(Ptr<Node> node, Ptr<L3Protocol> ndn,
229 Ptr<NetDevice> netDevice) const
Alexander Afanasyev122f3782013-02-02 00:04:40 -0800230{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800231 NS_LOG_DEBUG("Creating default NetDeviceFace on node " << node->GetId());
Alexander Afanasyev122f3782013-02-02 00:04:40 -0800232
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800233 shared_ptr<NetDeviceFace> face = std::make_shared<NetDeviceFace>(node, netDevice);
Alexander Afanasyevc17e4bd2013-02-17 14:31:56 -0800234
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800235 ndn->addFace(face);
236 NS_LOG_LOGIC("Node " << node->GetId() << ": added NetDeviceFace as face #"
237 << face->getLocalUri());
Alexander Afanasyevc17e4bd2013-02-17 14:31:56 -0800238
239 return face;
Alexander Afanasyev122f3782013-02-02 00:04:40 -0800240}
241
Spyridon Mastorakise4f0d3c2014-10-29 13:20:03 -0700242shared_ptr<NetDeviceFace>
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800243StackHelper::PointToPointNetDeviceCallback(Ptr<Node> node, Ptr<L3Protocol> ndn,
244 Ptr<NetDevice> device) const
Alexander Afanasyev122f3782013-02-02 00:04:40 -0800245{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800246 NS_LOG_DEBUG("Creating point-to-point NetDeviceFace on node " << node->GetId());
Alexander Afanasyev122f3782013-02-02 00:04:40 -0800247
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800248 shared_ptr<NetDeviceFace> face = std::make_shared<NetDeviceFace>(node, device);
Alexander Afanasyev122f3782013-02-02 00:04:40 -0800249
Spyridon Mastorakis9760bd02014-11-12 13:32:55 -0800250 ndn->addFace(face);
251 NS_LOG_LOGIC("Node " << node->GetId() << ": added NetDeviceFace as face #"
252 << face->getLocalUri());
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800253
Alexander Afanasyev122f3782013-02-02 00:04:40 -0800254 return face;
255}
256
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700257Ptr<FaceContainer>
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800258StackHelper::Install(const std::string& nodeName) const
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700259{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800260 Ptr<Node> node = Names::Find<Node>(nodeName);
261 return Install(node);
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700262}
263
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700264} // namespace ndn
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700265} // namespace ns3