blob: d1c2577de30479a7ac94df5cb2c639836146dc58 [file] [log] [blame]
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
Alexander Afanasyev45b92d42011-08-14 23:11:38 -07002/*
3 * Copyright (c) 2011 UCLA
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 *
Ilya Moiseenko25f7d4d2011-09-29 18:41:06 -070018 * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
Alexander Afanasyev122f3782013-02-02 00:04:40 -080019 * Ilya Moiseenko <iliamo@cs.ucla.edu>
Alexander Afanasyev45b92d42011-08-14 23:11:38 -070020 */
21
Alexander Afanasyev45b92d42011-08-14 23:11:38 -070022#include "ns3/assert.h"
23#include "ns3/log.h"
24#include "ns3/object.h"
25#include "ns3/names.h"
Alexander Afanasyev45b92d42011-08-14 23:11:38 -070026#include "ns3/packet-socket-factory.h"
27#include "ns3/config.h"
28#include "ns3/simulator.h"
29#include "ns3/string.h"
30#include "ns3/net-device.h"
Alexander Afanasyeva8f5d882012-11-09 14:22:48 -080031#include "ns3/channel.h"
Alexander Afanasyev45b92d42011-08-14 23:11:38 -070032#include "ns3/callback.h"
33#include "ns3/node.h"
34#include "ns3/core-config.h"
Alexander Afanasyev07827182011-12-13 01:07:32 -080035#include "ns3/point-to-point-net-device.h"
36#include "ns3/point-to-point-helper.h"
Alexander Afanasyev122f3782013-02-02 00:04:40 -080037#include "ns3/callback.h"
Alexander Afanasyev07827182011-12-13 01:07:32 -080038
Alexander Afanasyev0c395372014-12-20 15:54:02 -080039#include "../model/ndn-net-device-face.hpp"
40#include "../model/ndn-l3-protocol.hpp"
Alexander Afanasyev11f7bb42012-07-09 17:06:30 -070041
Alexander Afanasyev0c395372014-12-20 15:54:02 -080042#include "ns3/ndn-forwarding-strategy.hpp"
43#include "ns3/ndn-fib.hpp"
44#include "ns3/ndn-pit.hpp"
45#include "ns3/ndn-name.hpp"
46#include "ns3/ndn-content-store.hpp"
Alexander Afanasyev07827182011-12-13 01:07:32 -080047
Alexander Afanasyev52e9aa92011-11-15 20:23:20 -080048#include "ns3/node-list.h"
Alexander Afanasyev57bcbc32012-06-01 01:46:24 -070049// #include "ns3/loopback-net-device.h"
Alexander Afanasyevf9f4eb02011-12-16 01:51:14 -080050
Alexander Afanasyev11453142011-11-25 16:13:33 -080051#include "ns3/data-rate.h"
Alexander Afanasyev45b92d42011-08-14 23:11:38 -070052
Alexander Afanasyev0c395372014-12-20 15:54:02 -080053#include "ndn-face-container.hpp"
54#include "ndn-stack-helper.hpp"
Alexander Afanasyev45b92d42011-08-14 23:11:38 -070055
56#include <limits>
57#include <map>
Alexander Afanasyev52e9aa92011-11-15 20:23:20 -080058#include <boost/foreach.hpp>
Alexander Afanasyevb7626842012-01-12 13:43:33 -080059#include <boost/lexical_cast.hpp>
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -080060
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080061NS_LOG_COMPONENT_DEFINE("ndn.StackHelper");
Alexander Afanasyev45b92d42011-08-14 23:11:38 -070062
63namespace ns3 {
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070064namespace ndn {
Alexander Afanasyev122f3782013-02-02 00:04:40 -080065
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080066StackHelper::StackHelper()
67 : m_limitsEnabled(false)
68 , m_needSetDefaultRoutes(false)
Alexander Afanasyev45b92d42011-08-14 23:11:38 -070069{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080070 m_ndnFactory.SetTypeId("ns3::ndn::L3Protocol");
71 m_strategyFactory.SetTypeId("ns3::ndn::fw::Flooding");
72 m_contentStoreFactory.SetTypeId("ns3::ndn::cs::Lru");
73 m_fibFactory.SetTypeId("ns3::ndn::fib::Default");
74 m_pitFactory.SetTypeId("ns3::ndn::pit::Persistent");
Alexander Afanasyev122f3782013-02-02 00:04:40 -080075
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080076 m_netDeviceCallbacks.push_back(
77 std::make_pair(PointToPointNetDevice::GetTypeId(),
78 MakeCallback(&StackHelper::PointToPointNetDeviceCallback, this)));
Alexander Afanasyev122f3782013-02-02 00:04:40 -080079 // default callback will be fired if non of others callbacks fit or did the job
Ilya Moiseenko25f7d4d2011-09-29 18:41:06 -070080}
Alexander Afanasyev122f3782013-02-02 00:04:40 -080081
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080082StackHelper::~StackHelper()
Alexander Afanasyev45b92d42011-08-14 23:11:38 -070083{
Alexander Afanasyev45b92d42011-08-14 23:11:38 -070084}
85
Alexander Afanasyev3a4a0b32012-06-28 14:14:22 -070086void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080087StackHelper::SetStackAttributes(const std::string& attr1, const std::string& value1,
88 const std::string& attr2, const std::string& value2,
89 const std::string& attr3, const std::string& value3,
90 const std::string& attr4, const std::string& value4)
Alexander Afanasyev3a4a0b32012-06-28 14:14:22 -070091{
92 if (attr1 != "")
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080093 m_ndnFactory.Set(attr1, StringValue(value1));
Alexander Afanasyev3a4a0b32012-06-28 14:14:22 -070094 if (attr2 != "")
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080095 m_ndnFactory.Set(attr2, StringValue(value2));
Alexander Afanasyev3a4a0b32012-06-28 14:14:22 -070096 if (attr3 != "")
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080097 m_ndnFactory.Set(attr3, StringValue(value3));
Alexander Afanasyev3a4a0b32012-06-28 14:14:22 -070098 if (attr4 != "")
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080099 m_ndnFactory.Set(attr4, StringValue(value4));
Alexander Afanasyev3a4a0b32012-06-28 14:14:22 -0700100}
101
Alexander Afanasyev122f3782013-02-02 00:04:40 -0800102void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800103StackHelper::SetForwardingStrategy(const std::string& strategy, const std::string& attr1,
104 const std::string& value1, const std::string& attr2,
105 const std::string& value2, const std::string& attr3,
106 const std::string& value3, const std::string& attr4,
107 const std::string& value4)
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700108{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800109 m_strategyFactory.SetTypeId(strategy);
Alexander Afanasyev3a4a0b32012-06-28 14:14:22 -0700110 if (attr1 != "")
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800111 m_strategyFactory.Set(attr1, StringValue(value1));
Alexander Afanasyev3a4a0b32012-06-28 14:14:22 -0700112 if (attr2 != "")
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800113 m_strategyFactory.Set(attr2, StringValue(value2));
Alexander Afanasyev3a4a0b32012-06-28 14:14:22 -0700114 if (attr3 != "")
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800115 m_strategyFactory.Set(attr3, StringValue(value3));
Alexander Afanasyev3a4a0b32012-06-28 14:14:22 -0700116 if (attr4 != "")
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800117 m_strategyFactory.Set(attr4, StringValue(value4));
Alexander Afanasyev11453142011-11-25 16:13:33 -0800118}
119
Alexander Afanasyevd9fecdd2012-06-08 16:22:24 -0700120void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800121StackHelper::SetContentStore(const std::string& contentStore, const std::string& attr1,
122 const std::string& value1, const std::string& attr2,
123 const std::string& value2, const std::string& attr3,
124 const std::string& value3, const std::string& attr4,
125 const std::string& value4)
Alexander Afanasyevd9fecdd2012-06-08 16:22:24 -0700126{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800127 m_contentStoreFactory.SetTypeId(contentStore);
Alexander Afanasyev3a4a0b32012-06-28 14:14:22 -0700128 if (attr1 != "")
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800129 m_contentStoreFactory.Set(attr1, StringValue(value1));
Alexander Afanasyev3a4a0b32012-06-28 14:14:22 -0700130 if (attr2 != "")
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800131 m_contentStoreFactory.Set(attr2, StringValue(value2));
Alexander Afanasyev3a4a0b32012-06-28 14:14:22 -0700132 if (attr3 != "")
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800133 m_contentStoreFactory.Set(attr3, StringValue(value3));
Alexander Afanasyev3a4a0b32012-06-28 14:14:22 -0700134 if (attr4 != "")
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800135 m_contentStoreFactory.Set(attr4, StringValue(value4));
Alexander Afanasyev3a4a0b32012-06-28 14:14:22 -0700136}
137
138void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800139StackHelper::SetPit(const std::string& pitClass, const std::string& attr1,
140 const std::string& value1, const std::string& attr2, const std::string& value2,
141 const std::string& attr3, const std::string& value3, const std::string& attr4,
142 const std::string& value4)
Alexander Afanasyev3a4a0b32012-06-28 14:14:22 -0700143{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800144 m_pitFactory.SetTypeId(pitClass);
Alexander Afanasyev3a4a0b32012-06-28 14:14:22 -0700145 if (attr1 != "")
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800146 m_pitFactory.Set(attr1, StringValue(value1));
Alexander Afanasyev3a4a0b32012-06-28 14:14:22 -0700147 if (attr2 != "")
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800148 m_pitFactory.Set(attr2, StringValue(value2));
Alexander Afanasyev3a4a0b32012-06-28 14:14:22 -0700149 if (attr3 != "")
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800150 m_pitFactory.Set(attr3, StringValue(value3));
Alexander Afanasyev3a4a0b32012-06-28 14:14:22 -0700151 if (attr4 != "")
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800152 m_pitFactory.Set(attr4, StringValue(value4));
Alexander Afanasyev3a4a0b32012-06-28 14:14:22 -0700153}
154
155void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800156StackHelper::SetFib(const std::string& fibClass, const std::string& attr1,
157 const std::string& value1, const std::string& attr2, const std::string& value2,
158 const std::string& attr3, const std::string& value3, const std::string& attr4,
159 const std::string& value4)
Alexander Afanasyev3a4a0b32012-06-28 14:14:22 -0700160{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800161 m_fibFactory.SetTypeId(fibClass);
Alexander Afanasyev3a4a0b32012-06-28 14:14:22 -0700162 if (attr1 != "")
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800163 m_fibFactory.Set(attr1, StringValue(value1));
Alexander Afanasyev3a4a0b32012-06-28 14:14:22 -0700164 if (attr2 != "")
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800165 m_fibFactory.Set(attr2, StringValue(value2));
Alexander Afanasyev3a4a0b32012-06-28 14:14:22 -0700166 if (attr3 != "")
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800167 m_fibFactory.Set(attr3, StringValue(value3));
Alexander Afanasyev3a4a0b32012-06-28 14:14:22 -0700168 if (attr4 != "")
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800169 m_fibFactory.Set(attr4, StringValue(value4));
Alexander Afanasyevd9fecdd2012-06-08 16:22:24 -0700170}
Alexander Afanasyev4a5c2c12011-12-12 18:50:57 -0800171
172void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800173StackHelper::SetDefaultRoutes(bool needSet)
Alexander Afanasyev4a5c2c12011-12-12 18:50:57 -0800174{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800175 NS_LOG_FUNCTION(this << needSet);
Alexander Afanasyev4a5c2c12011-12-12 18:50:57 -0800176 m_needSetDefaultRoutes = needSet;
177}
178
Alexander Afanasyev11453142011-11-25 16:13:33 -0800179void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800180StackHelper::EnableLimits(bool enable /* = true*/, Time avgRtt /*=Seconds(0.1)*/,
181 uint32_t avgData /*=1100*/, uint32_t avgInterest /*=40*/)
Alexander Afanasyev11453142011-11-25 16:13:33 -0800182{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800183 NS_LOG_INFO("EnableLimits: " << enable);
Alexander Afanasyev11453142011-11-25 16:13:33 -0800184 m_limitsEnabled = enable;
Alexander Afanasyev8f5a9bb2011-12-18 19:49:02 -0800185 m_avgRtt = avgRtt;
Alexander Afanasyev772f51b2013-08-01 18:53:25 -0700186 m_avgDataSize = avgData;
Alexander Afanasyevc39f0b42011-11-28 12:51:12 -0800187 m_avgInterestSize = avgInterest;
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700188}
189
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700190Ptr<FaceContainer>
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800191StackHelper::Install(const NodeContainer& c) const
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700192{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800193 Ptr<FaceContainer> faces = Create<FaceContainer>();
194 for (NodeContainer::Iterator i = c.Begin(); i != c.End(); ++i) {
195 faces->AddAll(Install(*i));
196 }
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700197 return faces;
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700198}
199
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700200Ptr<FaceContainer>
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800201StackHelper::InstallAll() const
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700202{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800203 return Install(NodeContainer::GetGlobal());
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700204}
205
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700206Ptr<FaceContainer>
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800207StackHelper::Install(Ptr<Node> node) const
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700208{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800209 // NS_ASSERT_MSG (m_forwarding, "SetForwardingHelper() should be set prior calling Install()
210 // method");
211 Ptr<FaceContainer> faces = Create<FaceContainer>();
Alexander Afanasyev122f3782013-02-02 00:04:40 -0800212
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800213 if (node->GetObject<L3Protocol>() != 0) {
214 NS_FATAL_ERROR("StackHelper::Install (): Installing "
215 "a NdnStack to a node with an existing Ndn object");
216 return 0;
217 }
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700218
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700219 // Create L3Protocol
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800220 Ptr<L3Protocol> ndn = m_ndnFactory.Create<L3Protocol>();
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700221
Alexander Afanasyev3a4a0b32012-06-28 14:14:22 -0700222 // Create and aggregate FIB
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800223 Ptr<Fib> fib = m_fibFactory.Create<Fib>();
224 ndn->AggregateObject(fib);
Alexander Afanasyev3a4a0b32012-06-28 14:14:22 -0700225
226 // Create and aggregate PIT
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800227 ndn->AggregateObject(m_pitFactory.Create<Pit>());
Alexander Afanasyev122f3782013-02-02 00:04:40 -0800228
Alexander Afanasyev3a4a0b32012-06-28 14:14:22 -0700229 // Create and aggregate forwarding strategy
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800230 ndn->AggregateObject(m_strategyFactory.Create<ForwardingStrategy>());
Alexander Afanasyev3a4a0b32012-06-28 14:14:22 -0700231
232 // Create and aggregate content store
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800233 ndn->AggregateObject(m_contentStoreFactory.Create<ContentStore>());
Alexander Afanasyev3a4a0b32012-06-28 14:14:22 -0700234
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700235 // Aggregate L3Protocol on node
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800236 node->AggregateObject(ndn);
Alexander Afanasyev122f3782013-02-02 00:04:40 -0800237
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800238 for (uint32_t index = 0; index < node->GetNDevices(); index++) {
239 Ptr<NetDevice> device = node->GetDevice(index);
240 // This check does not make sense: LoopbackNetDevice is installed only if IP stack is installed,
241 // Normally, ndnSIM works without IP stack, so no reason to check
242 // if (DynamicCast<LoopbackNetDevice> (device) != 0)
243 // continue; // don't create face for a LoopbackNetDevice
Alexander Afanasyev11453142011-11-25 16:13:33 -0800244
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800245 Ptr<NetDeviceFace> face;
Alexander Afanasyev122f3782013-02-02 00:04:40 -0800246
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800247 for (std::list<std::pair<TypeId, NetDeviceFaceCreateCallback>>::const_iterator item =
248 m_netDeviceCallbacks.begin();
249 item != m_netDeviceCallbacks.end(); item++) {
250 if (device->GetInstanceTypeId() == item->first
251 || device->GetInstanceTypeId().IsChildOf(item->first)) {
252 face = item->second(node, ndn, device);
253 if (face != 0)
254 break;
255 }
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700256 }
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800257 if (face == 0) {
258 face = DefaultNetDeviceCallback(node, ndn, device);
259 }
260
261 if (m_needSetDefaultRoutes) {
262 // default route with lowest priority possible
263 AddRoute(node, "/", StaticCast<Face>(face), std::numeric_limits<int32_t>::max());
264 }
265
266 face->SetUp();
267 faces->Add(face);
268 }
Alexander Afanasyev122f3782013-02-02 00:04:40 -0800269
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700270 return faces;
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700271}
272
Alexander Afanasyev122f3782013-02-02 00:04:40 -0800273void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800274StackHelper::AddNetDeviceFaceCreateCallback(TypeId netDeviceType,
275 StackHelper::NetDeviceFaceCreateCallback callback)
Alexander Afanasyev122f3782013-02-02 00:04:40 -0800276{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800277 m_netDeviceCallbacks.push_back(std::make_pair(netDeviceType, callback));
Alexander Afanasyev122f3782013-02-02 00:04:40 -0800278}
279
Alexander Afanasyev2a269f72013-06-06 22:59:33 -0700280void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800281StackHelper::UpdateNetDeviceFaceCreateCallback(TypeId netDeviceType,
282 NetDeviceFaceCreateCallback callback)
Alexander Afanasyev2a269f72013-06-06 22:59:33 -0700283{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800284 for (NetDeviceCallbackList::iterator i = m_netDeviceCallbacks.begin();
285 i != m_netDeviceCallbacks.end(); i++) {
286 if (i->first == netDeviceType) {
287 i->second = callback;
288 return;
Alexander Afanasyev2a269f72013-06-06 22:59:33 -0700289 }
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800290 }
Alexander Afanasyev2a269f72013-06-06 22:59:33 -0700291}
292
293void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800294StackHelper::RemoveNetDeviceFaceCreateCallback(TypeId netDeviceType,
295 NetDeviceFaceCreateCallback callback)
Alexander Afanasyev2a269f72013-06-06 22:59:33 -0700296{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800297 for (NetDeviceCallbackList::iterator i = m_netDeviceCallbacks.begin();
298 i != m_netDeviceCallbacks.end(); i++) {
299 if (i->first == netDeviceType) {
300 m_netDeviceCallbacks.erase(i);
301 return;
Alexander Afanasyev2a269f72013-06-06 22:59:33 -0700302 }
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800303 }
Alexander Afanasyev2a269f72013-06-06 22:59:33 -0700304}
Alexander Afanasyev122f3782013-02-02 00:04:40 -0800305
306Ptr<NetDeviceFace>
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800307StackHelper::DefaultNetDeviceCallback(Ptr<Node> node, Ptr<L3Protocol> ndn,
308 Ptr<NetDevice> netDevice) const
Alexander Afanasyev122f3782013-02-02 00:04:40 -0800309{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800310 NS_LOG_DEBUG("Creating default NetDeviceFace on node " << node->GetId());
Alexander Afanasyev122f3782013-02-02 00:04:40 -0800311
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800312 Ptr<NetDeviceFace> face = CreateObject<NetDeviceFace>(node, netDevice);
Alexander Afanasyevc17e4bd2013-02-17 14:31:56 -0800313
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800314 ndn->AddFace(face);
315 NS_LOG_LOGIC("Node " << node->GetId() << ": added NetDeviceFace as face #" << *face);
Alexander Afanasyevc17e4bd2013-02-17 14:31:56 -0800316
317 return face;
Alexander Afanasyev122f3782013-02-02 00:04:40 -0800318}
319
320Ptr<NetDeviceFace>
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800321StackHelper::PointToPointNetDeviceCallback(Ptr<Node> node, Ptr<L3Protocol> ndn,
322 Ptr<NetDevice> device) const
Alexander Afanasyev122f3782013-02-02 00:04:40 -0800323{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800324 NS_LOG_DEBUG("Creating point-to-point NetDeviceFace on node " << node->GetId());
Alexander Afanasyev122f3782013-02-02 00:04:40 -0800325
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800326 Ptr<NetDeviceFace> face = CreateObject<NetDeviceFace>(node, device);
Alexander Afanasyev122f3782013-02-02 00:04:40 -0800327
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800328 ndn->AddFace(face);
329 NS_LOG_LOGIC("Node " << node->GetId() << ": added NetDeviceFace as face #" << *face);
Alexander Afanasyevc17e4bd2013-02-17 14:31:56 -0800330
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800331 if (m_limitsEnabled) {
332 Ptr<Limits> limits = face->GetObject<Limits>();
333 if (limits == 0) {
334 NS_FATAL_ERROR("Limits are enabled, but the selected forwarding strategy does not support "
335 "limits. Please revise your scenario");
336 exit(1);
Alexander Afanasyev122f3782013-02-02 00:04:40 -0800337 }
338
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800339 NS_LOG_INFO("Limits are enabled");
340 Ptr<PointToPointNetDevice> p2p = DynamicCast<PointToPointNetDevice>(device);
341 if (p2p != 0) {
342 // Setup bucket filtering
343 // Assume that we know average data packet size, and this size is equal default size
344 // Set maximum buckets (averaging over 1 second)
345
346 DataRateValue dataRate;
347 device->GetAttribute("DataRate", dataRate);
348 TimeValue linkDelay;
349 device->GetChannel()->GetAttribute("Delay", linkDelay);
350
351 NS_LOG_INFO("DataRate for this link is " << dataRate.Get());
352
353 double maxInterestPackets =
354 1.0 * dataRate.Get().GetBitRate() / 8.0 / (m_avgDataSize + m_avgInterestSize);
355 // NS_LOG_INFO ("Max packets per second: " << maxInterestPackets);
356 // NS_LOG_INFO ("Max burst: " << m_avgRtt.ToDouble (Time::S) * maxInterestPackets);
357 NS_LOG_INFO("MaxLimit: " << (int)(m_avgRtt.ToDouble(Time::S) * maxInterestPackets));
358
359 // Set max to BDP
360 limits->SetLimits(maxInterestPackets, m_avgRtt.ToDouble(Time::S));
361 limits->SetLinkDelay(linkDelay.Get().ToDouble(Time::S));
362 }
363 }
364
Alexander Afanasyev122f3782013-02-02 00:04:40 -0800365 return face;
366}
367
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700368Ptr<FaceContainer>
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800369StackHelper::Install(const std::string& nodeName) const
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700370{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800371 Ptr<Node> node = Names::Find<Node>(nodeName);
372 return Install(node);
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700373}
374
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700375void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800376StackHelper::AddRoute(Ptr<Node> node, const std::string& prefix, Ptr<Face> face, int32_t metric)
Alexander Afanasyev52e9aa92011-11-15 20:23:20 -0800377{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800378 NS_LOG_LOGIC("[" << node->GetId() << "]$ route add " << prefix << " via " << *face << " metric "
379 << metric);
Alexander Afanasyev4a5c2c12011-12-12 18:50:57 -0800380
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800381 Ptr<Fib> fib = node->GetObject<Fib>();
Alexander Afanasyev52e9aa92011-11-15 20:23:20 -0800382
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -0700383 NameValue prefixValue;
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800384 prefixValue.DeserializeFromString(prefix, MakeNameChecker());
385 fib->Add(prefixValue.Get(), face, metric);
Alexander Afanasyev52e9aa92011-11-15 20:23:20 -0800386}
387
388void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800389StackHelper::AddRoute(Ptr<Node> node, const std::string& prefix, uint32_t faceId, int32_t metric)
Alexander Afanasyev8bedcaf2012-07-20 15:30:44 -0700390{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800391 Ptr<L3Protocol> ndn = node->GetObject<L3Protocol>();
392 NS_ASSERT_MSG(ndn != 0, "Ndn stack should be installed on the node");
Alexander Afanasyev8bedcaf2012-07-20 15:30:44 -0700393
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800394 Ptr<Face> face = ndn->GetFace(faceId);
395 NS_ASSERT_MSG(face != 0, "Face with ID [" << faceId << "] does not exist on node ["
396 << node->GetId() << "]");
Alexander Afanasyev8bedcaf2012-07-20 15:30:44 -0700397
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800398 AddRoute(node, prefix, face, metric);
Alexander Afanasyev8bedcaf2012-07-20 15:30:44 -0700399}
400
401void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800402StackHelper::AddRoute(const std::string& nodeName, const std::string& prefix, uint32_t faceId,
403 int32_t metric)
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700404{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800405 Ptr<Node> node = Names::Find<Node>(nodeName);
406 NS_ASSERT_MSG(node != 0, "Node [" << nodeName << "] does not exist");
Alexander Afanasyev122f3782013-02-02 00:04:40 -0800407
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800408 Ptr<L3Protocol> ndn = node->GetObject<L3Protocol>();
409 NS_ASSERT_MSG(ndn != 0, "Ndn stack should be installed on the node");
Alexander Afanasyev52e9aa92011-11-15 20:23:20 -0800410
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800411 Ptr<Face> face = ndn->GetFace(faceId);
412 NS_ASSERT_MSG(face != 0, "Face with ID [" << faceId << "] does not exist on node [" << nodeName
413 << "]");
Alexander Afanasyev52e9aa92011-11-15 20:23:20 -0800414
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800415 AddRoute(node, prefix, face, metric);
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700416}
Alexander Afanasyev4a5c2c12011-12-12 18:50:57 -0800417
Alexander Afanasyevfd258262012-11-23 14:59:19 -0800418void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800419StackHelper::AddRoute(Ptr<Node> node, const std::string& prefix, Ptr<Node> otherNode,
420 int32_t metric)
Alexander Afanasyevfd258262012-11-23 14:59:19 -0800421{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800422 for (uint32_t deviceId = 0; deviceId < node->GetNDevices(); deviceId++) {
423 Ptr<PointToPointNetDevice> netDevice =
424 DynamicCast<PointToPointNetDevice>(node->GetDevice(deviceId));
425 if (netDevice == 0)
426 continue;
Alexander Afanasyevfd258262012-11-23 14:59:19 -0800427
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800428 Ptr<Channel> channel = netDevice->GetChannel();
429 if (channel == 0)
430 continue;
Alexander Afanasyevfd258262012-11-23 14:59:19 -0800431
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800432 if (channel->GetDevice(0)->GetNode() == otherNode
433 || channel->GetDevice(1)->GetNode() == otherNode) {
434 Ptr<L3Protocol> ndn = node->GetObject<L3Protocol>();
435 NS_ASSERT_MSG(ndn != 0, "Ndn stack should be installed on the node");
Alexander Afanasyevfd258262012-11-23 14:59:19 -0800436
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800437 Ptr<Face> face = ndn->GetFaceByNetDevice(netDevice);
438 NS_ASSERT_MSG(face != 0, "There is no face associated with the p2p link");
Alexander Afanasyevfd258262012-11-23 14:59:19 -0800439
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800440 AddRoute(node, prefix, face, metric);
Alexander Afanasyevfd258262012-11-23 14:59:19 -0800441
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800442 return;
Alexander Afanasyevfd258262012-11-23 14:59:19 -0800443 }
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800444 }
Alexander Afanasyevfd258262012-11-23 14:59:19 -0800445
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800446 NS_FATAL_ERROR("Cannot add route: Node# " << node->GetId() << " and Node# " << otherNode->GetId()
447 << " are not connected");
Alexander Afanasyevfd258262012-11-23 14:59:19 -0800448}
449
450void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800451StackHelper::AddRoute(const std::string& nodeName, const std::string& prefix,
452 const std::string& otherNodeName, int32_t metric)
Alexander Afanasyevfd258262012-11-23 14:59:19 -0800453{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800454 Ptr<Node> node = Names::Find<Node>(nodeName);
455 NS_ASSERT_MSG(node != 0, "Node [" << nodeName << "] does not exist");
Alexander Afanasyevfd258262012-11-23 14:59:19 -0800456
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800457 Ptr<Node> otherNode = Names::Find<Node>(otherNodeName);
458 NS_ASSERT_MSG(otherNode != 0, "Node [" << otherNodeName << "] does not exist");
Alexander Afanasyevfd258262012-11-23 14:59:19 -0800459
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800460 AddRoute(node, prefix, otherNode, metric);
Alexander Afanasyevfd258262012-11-23 14:59:19 -0800461}
462
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700463} // namespace ndn
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700464} // namespace ns3