blob: b5488440b1bdb1035730afab8c390726ecb05b55 [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 Afanasyev52e9aa92011-11-15 20:23:20 -080042#include "ns3/node-list.h"
Alexander Afanasyevf9f4eb02011-12-16 01:51:14 -080043
Alexander Afanasyev11453142011-11-25 16:13:33 -080044#include "ns3/data-rate.h"
Alexander Afanasyev45b92d42011-08-14 23:11:38 -070045
Alexander Afanasyev0c395372014-12-20 15:54:02 -080046#include "ndn-face-container.hpp"
47#include "ndn-stack-helper.hpp"
Alexander Afanasyev45b92d42011-08-14 23:11:38 -070048
49#include <limits>
50#include <map>
Alexander Afanasyev52e9aa92011-11-15 20:23:20 -080051#include <boost/foreach.hpp>
Alexander Afanasyevb7626842012-01-12 13:43:33 -080052#include <boost/lexical_cast.hpp>
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -080053
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080054NS_LOG_COMPONENT_DEFINE("ndn.StackHelper");
Alexander Afanasyev45b92d42011-08-14 23:11:38 -070055
56namespace ns3 {
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070057namespace ndn {
Alexander Afanasyev122f3782013-02-02 00:04:40 -080058
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080059StackHelper::StackHelper()
60 : m_limitsEnabled(false)
61 , m_needSetDefaultRoutes(false)
Alexander Afanasyev45b92d42011-08-14 23:11:38 -070062{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080063 m_ndnFactory.SetTypeId("ns3::ndn::L3Protocol");
64 m_strategyFactory.SetTypeId("ns3::ndn::fw::Flooding");
65 m_contentStoreFactory.SetTypeId("ns3::ndn::cs::Lru");
66 m_fibFactory.SetTypeId("ns3::ndn::fib::Default");
67 m_pitFactory.SetTypeId("ns3::ndn::pit::Persistent");
Alexander Afanasyev122f3782013-02-02 00:04:40 -080068
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080069 m_netDeviceCallbacks.push_back(
70 std::make_pair(PointToPointNetDevice::GetTypeId(),
71 MakeCallback(&StackHelper::PointToPointNetDeviceCallback, this)));
Alexander Afanasyev122f3782013-02-02 00:04:40 -080072 // default callback will be fired if non of others callbacks fit or did the job
Ilya Moiseenko25f7d4d2011-09-29 18:41:06 -070073}
Alexander Afanasyev122f3782013-02-02 00:04:40 -080074
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080075StackHelper::~StackHelper()
Alexander Afanasyev45b92d42011-08-14 23:11:38 -070076{
Alexander Afanasyev45b92d42011-08-14 23:11:38 -070077}
78
Alexander Afanasyev3a4a0b32012-06-28 14:14:22 -070079void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080080StackHelper::SetStackAttributes(const std::string& attr1, const std::string& value1,
81 const std::string& attr2, const std::string& value2,
82 const std::string& attr3, const std::string& value3,
83 const std::string& attr4, const std::string& value4)
Alexander Afanasyev3a4a0b32012-06-28 14:14:22 -070084{
85 if (attr1 != "")
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080086 m_ndnFactory.Set(attr1, StringValue(value1));
Alexander Afanasyev3a4a0b32012-06-28 14:14:22 -070087 if (attr2 != "")
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080088 m_ndnFactory.Set(attr2, StringValue(value2));
Alexander Afanasyev3a4a0b32012-06-28 14:14:22 -070089 if (attr3 != "")
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080090 m_ndnFactory.Set(attr3, StringValue(value3));
Alexander Afanasyev3a4a0b32012-06-28 14:14:22 -070091 if (attr4 != "")
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080092 m_ndnFactory.Set(attr4, StringValue(value4));
Alexander Afanasyev3a4a0b32012-06-28 14:14:22 -070093}
94
Alexander Afanasyev122f3782013-02-02 00:04:40 -080095void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080096StackHelper::SetForwardingStrategy(const std::string& strategy, const std::string& attr1,
97 const std::string& value1, const std::string& attr2,
98 const std::string& value2, const std::string& attr3,
99 const std::string& value3, const std::string& attr4,
100 const std::string& value4)
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700101{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800102 m_strategyFactory.SetTypeId(strategy);
Alexander Afanasyev3a4a0b32012-06-28 14:14:22 -0700103 if (attr1 != "")
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800104 m_strategyFactory.Set(attr1, StringValue(value1));
Alexander Afanasyev3a4a0b32012-06-28 14:14:22 -0700105 if (attr2 != "")
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800106 m_strategyFactory.Set(attr2, StringValue(value2));
Alexander Afanasyev3a4a0b32012-06-28 14:14:22 -0700107 if (attr3 != "")
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800108 m_strategyFactory.Set(attr3, StringValue(value3));
Alexander Afanasyev3a4a0b32012-06-28 14:14:22 -0700109 if (attr4 != "")
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800110 m_strategyFactory.Set(attr4, StringValue(value4));
Alexander Afanasyev11453142011-11-25 16:13:33 -0800111}
112
Alexander Afanasyevd9fecdd2012-06-08 16:22:24 -0700113void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800114StackHelper::SetContentStore(const std::string& contentStore, const std::string& attr1,
115 const std::string& value1, const std::string& attr2,
116 const std::string& value2, const std::string& attr3,
117 const std::string& value3, const std::string& attr4,
118 const std::string& value4)
Alexander Afanasyevd9fecdd2012-06-08 16:22:24 -0700119{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800120 m_contentStoreFactory.SetTypeId(contentStore);
Alexander Afanasyev3a4a0b32012-06-28 14:14:22 -0700121 if (attr1 != "")
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800122 m_contentStoreFactory.Set(attr1, StringValue(value1));
Alexander Afanasyev3a4a0b32012-06-28 14:14:22 -0700123 if (attr2 != "")
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800124 m_contentStoreFactory.Set(attr2, StringValue(value2));
Alexander Afanasyev3a4a0b32012-06-28 14:14:22 -0700125 if (attr3 != "")
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800126 m_contentStoreFactory.Set(attr3, StringValue(value3));
Alexander Afanasyev3a4a0b32012-06-28 14:14:22 -0700127 if (attr4 != "")
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800128 m_contentStoreFactory.Set(attr4, StringValue(value4));
Alexander Afanasyev3a4a0b32012-06-28 14:14:22 -0700129}
130
131void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800132StackHelper::SetPit(const std::string& pitClass, const std::string& attr1,
133 const std::string& value1, const std::string& attr2, const std::string& value2,
134 const std::string& attr3, const std::string& value3, const std::string& attr4,
135 const std::string& value4)
Alexander Afanasyev3a4a0b32012-06-28 14:14:22 -0700136{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800137 m_pitFactory.SetTypeId(pitClass);
Alexander Afanasyev3a4a0b32012-06-28 14:14:22 -0700138 if (attr1 != "")
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800139 m_pitFactory.Set(attr1, StringValue(value1));
Alexander Afanasyev3a4a0b32012-06-28 14:14:22 -0700140 if (attr2 != "")
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800141 m_pitFactory.Set(attr2, StringValue(value2));
Alexander Afanasyev3a4a0b32012-06-28 14:14:22 -0700142 if (attr3 != "")
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800143 m_pitFactory.Set(attr3, StringValue(value3));
Alexander Afanasyev3a4a0b32012-06-28 14:14:22 -0700144 if (attr4 != "")
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800145 m_pitFactory.Set(attr4, StringValue(value4));
Alexander Afanasyev3a4a0b32012-06-28 14:14:22 -0700146}
147
148void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800149StackHelper::SetFib(const std::string& fibClass, const std::string& attr1,
150 const std::string& value1, const std::string& attr2, const std::string& value2,
151 const std::string& attr3, const std::string& value3, const std::string& attr4,
152 const std::string& value4)
Alexander Afanasyev3a4a0b32012-06-28 14:14:22 -0700153{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800154 m_fibFactory.SetTypeId(fibClass);
Alexander Afanasyev3a4a0b32012-06-28 14:14:22 -0700155 if (attr1 != "")
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800156 m_fibFactory.Set(attr1, StringValue(value1));
Alexander Afanasyev3a4a0b32012-06-28 14:14:22 -0700157 if (attr2 != "")
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800158 m_fibFactory.Set(attr2, StringValue(value2));
Alexander Afanasyev3a4a0b32012-06-28 14:14:22 -0700159 if (attr3 != "")
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800160 m_fibFactory.Set(attr3, StringValue(value3));
Alexander Afanasyev3a4a0b32012-06-28 14:14:22 -0700161 if (attr4 != "")
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800162 m_fibFactory.Set(attr4, StringValue(value4));
Alexander Afanasyevd9fecdd2012-06-08 16:22:24 -0700163}
Alexander Afanasyev4a5c2c12011-12-12 18:50:57 -0800164
165void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800166StackHelper::SetDefaultRoutes(bool needSet)
Alexander Afanasyev4a5c2c12011-12-12 18:50:57 -0800167{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800168 NS_LOG_FUNCTION(this << needSet);
Alexander Afanasyev4a5c2c12011-12-12 18:50:57 -0800169 m_needSetDefaultRoutes = needSet;
170}
171
Alexander Afanasyev11453142011-11-25 16:13:33 -0800172void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800173StackHelper::EnableLimits(bool enable /* = true*/, Time avgRtt /*=Seconds(0.1)*/,
174 uint32_t avgData /*=1100*/, uint32_t avgInterest /*=40*/)
Alexander Afanasyev11453142011-11-25 16:13:33 -0800175{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800176 NS_LOG_INFO("EnableLimits: " << enable);
Alexander Afanasyev11453142011-11-25 16:13:33 -0800177 m_limitsEnabled = enable;
Alexander Afanasyev8f5a9bb2011-12-18 19:49:02 -0800178 m_avgRtt = avgRtt;
Alexander Afanasyev772f51b2013-08-01 18:53:25 -0700179 m_avgDataSize = avgData;
Alexander Afanasyevc39f0b42011-11-28 12:51:12 -0800180 m_avgInterestSize = avgInterest;
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700181}
182
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700183Ptr<FaceContainer>
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800184StackHelper::Install(const NodeContainer& c) const
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700185{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800186 Ptr<FaceContainer> faces = Create<FaceContainer>();
187 for (NodeContainer::Iterator i = c.Begin(); i != c.End(); ++i) {
188 faces->AddAll(Install(*i));
189 }
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700190 return faces;
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700191}
192
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700193Ptr<FaceContainer>
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800194StackHelper::InstallAll() const
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700195{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800196 return Install(NodeContainer::GetGlobal());
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700197}
198
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700199Ptr<FaceContainer>
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800200StackHelper::Install(Ptr<Node> node) const
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700201{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800202 // NS_ASSERT_MSG (m_forwarding, "SetForwardingHelper() should be set prior calling Install()
203 // method");
204 Ptr<FaceContainer> faces = Create<FaceContainer>();
Alexander Afanasyev122f3782013-02-02 00:04:40 -0800205
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800206 if (node->GetObject<L3Protocol>() != 0) {
207 NS_FATAL_ERROR("StackHelper::Install (): Installing "
208 "a NdnStack to a node with an existing Ndn object");
209 return 0;
210 }
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700211
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700212 // Create L3Protocol
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800213 Ptr<L3Protocol> ndn = m_ndnFactory.Create<L3Protocol>();
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700214
Alexander Afanasyev3a4a0b32012-06-28 14:14:22 -0700215 // Create and aggregate FIB
Spyridon Mastorakise4f0d3c2014-10-29 13:20:03 -0700216 // Ptr<Fib> fib = m_fibFactory.Create<Fib> ();
217 // ndn->AggregateObject (fib);
Alexander Afanasyev3a4a0b32012-06-28 14:14:22 -0700218
219 // Create and aggregate PIT
Spyridon Mastorakise4f0d3c2014-10-29 13:20:03 -0700220 // ndn->AggregateObject (m_pitFactory.Create<Pit> ());
Alexander Afanasyev122f3782013-02-02 00:04:40 -0800221
Alexander Afanasyev3a4a0b32012-06-28 14:14:22 -0700222 // Create and aggregate forwarding strategy
Spyridon Mastorakise4f0d3c2014-10-29 13:20:03 -0700223 // ndn->AggregateObject (m_strategyFactory.Create<ForwardingStrategy> ());
Alexander Afanasyev3a4a0b32012-06-28 14:14:22 -0700224
225 // Create and aggregate content store
Spyridon Mastorakise4f0d3c2014-10-29 13:20:03 -0700226 // ndn->AggregateObject (m_contentStoreFactory.Create<ContentStore> ());
Alexander Afanasyev3a4a0b32012-06-28 14:14:22 -0700227
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700228 // Aggregate L3Protocol on node
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800229 node->AggregateObject(ndn);
Alexander Afanasyev122f3782013-02-02 00:04:40 -0800230
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800231 for (uint32_t index = 0; index < node->GetNDevices(); index++) {
232 Ptr<NetDevice> device = node->GetDevice(index);
233 // This check does not make sense: LoopbackNetDevice is installed only if IP stack is installed,
234 // Normally, ndnSIM works without IP stack, so no reason to check
235 // if (DynamicCast<LoopbackNetDevice> (device) != 0)
236 // continue; // don't create face for a LoopbackNetDevice
Alexander Afanasyev11453142011-11-25 16:13:33 -0800237
Spyridon Mastorakise4f0d3c2014-10-29 13:20:03 -0700238 shared_ptr<NetDeviceFace> face;
Alexander Afanasyev122f3782013-02-02 00:04:40 -0800239
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800240 for (std::list<std::pair<TypeId, NetDeviceFaceCreateCallback>>::const_iterator item =
241 m_netDeviceCallbacks.begin();
242 item != m_netDeviceCallbacks.end(); item++) {
243 if (device->GetInstanceTypeId() == item->first
244 || device->GetInstanceTypeId().IsChildOf(item->first)) {
245 face = item->second(node, ndn, device);
246 if (face != 0)
247 break;
248 }
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700249 }
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800250 if (face == 0) {
251 face = DefaultNetDeviceCallback(node, ndn, device);
252 }
253
254 if (m_needSetDefaultRoutes) {
255 // default route with lowest priority possible
256 AddRoute(node, "/", StaticCast<Face>(face), std::numeric_limits<int32_t>::max());
257 }
258
259 face->SetUp();
260 faces->Add(face);
261 }
Alexander Afanasyev122f3782013-02-02 00:04:40 -0800262
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700263 return faces;
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700264}
265
Alexander Afanasyev122f3782013-02-02 00:04:40 -0800266void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800267StackHelper::AddNetDeviceFaceCreateCallback(TypeId netDeviceType,
268 StackHelper::NetDeviceFaceCreateCallback callback)
Alexander Afanasyev122f3782013-02-02 00:04:40 -0800269{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800270 m_netDeviceCallbacks.push_back(std::make_pair(netDeviceType, callback));
Alexander Afanasyev122f3782013-02-02 00:04:40 -0800271}
272
Alexander Afanasyev2a269f72013-06-06 22:59:33 -0700273void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800274StackHelper::UpdateNetDeviceFaceCreateCallback(TypeId netDeviceType,
275 NetDeviceFaceCreateCallback callback)
Alexander Afanasyev2a269f72013-06-06 22:59:33 -0700276{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800277 for (NetDeviceCallbackList::iterator i = m_netDeviceCallbacks.begin();
278 i != m_netDeviceCallbacks.end(); i++) {
279 if (i->first == netDeviceType) {
280 i->second = callback;
281 return;
Alexander Afanasyev2a269f72013-06-06 22:59:33 -0700282 }
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800283 }
Alexander Afanasyev2a269f72013-06-06 22:59:33 -0700284}
285
286void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800287StackHelper::RemoveNetDeviceFaceCreateCallback(TypeId netDeviceType,
288 NetDeviceFaceCreateCallback callback)
Alexander Afanasyev2a269f72013-06-06 22:59:33 -0700289{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800290 for (NetDeviceCallbackList::iterator i = m_netDeviceCallbacks.begin();
291 i != m_netDeviceCallbacks.end(); i++) {
292 if (i->first == netDeviceType) {
293 m_netDeviceCallbacks.erase(i);
294 return;
Alexander Afanasyev2a269f72013-06-06 22:59:33 -0700295 }
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800296 }
Alexander Afanasyev2a269f72013-06-06 22:59:33 -0700297}
Alexander Afanasyev122f3782013-02-02 00:04:40 -0800298
Spyridon Mastorakise4f0d3c2014-10-29 13:20:03 -0700299shared_ptr<NetDeviceFace>
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800300StackHelper::DefaultNetDeviceCallback(Ptr<Node> node, Ptr<L3Protocol> ndn,
301 Ptr<NetDevice> netDevice) const
Alexander Afanasyev122f3782013-02-02 00:04:40 -0800302{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800303 NS_LOG_DEBUG("Creating default NetDeviceFace on node " << node->GetId());
Alexander Afanasyev122f3782013-02-02 00:04:40 -0800304
Spyridon Mastorakise4f0d3c2014-10-29 13:20:03 -0700305 shared_ptr<NetDeviceFace> face = CreateObject<NetDeviceFace>(node, netDevice);
Alexander Afanasyevc17e4bd2013-02-17 14:31:56 -0800306
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800307 ndn->AddFace(face);
308 NS_LOG_LOGIC("Node " << node->GetId() << ": added NetDeviceFace as face #" << *face);
Alexander Afanasyevc17e4bd2013-02-17 14:31:56 -0800309
310 return face;
Alexander Afanasyev122f3782013-02-02 00:04:40 -0800311}
312
Spyridon Mastorakise4f0d3c2014-10-29 13:20:03 -0700313shared_ptr<NetDeviceFace>
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800314StackHelper::PointToPointNetDeviceCallback(Ptr<Node> node, Ptr<L3Protocol> ndn,
315 Ptr<NetDevice> device) const
Alexander Afanasyev122f3782013-02-02 00:04:40 -0800316{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800317 NS_LOG_DEBUG("Creating point-to-point NetDeviceFace on node " << node->GetId());
Alexander Afanasyev122f3782013-02-02 00:04:40 -0800318
Spyridon Mastorakise4f0d3c2014-10-29 13:20:03 -0700319 shared_ptr<NetDeviceFace> face = CreateObject<NetDeviceFace>(node, device);
Alexander Afanasyev122f3782013-02-02 00:04:40 -0800320
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800321 ndn->AddFace(face);
322 NS_LOG_LOGIC("Node " << node->GetId() << ": added NetDeviceFace as face #" << *face);
Alexander Afanasyevc17e4bd2013-02-17 14:31:56 -0800323
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800324 if (m_limitsEnabled) {
Spyridon Mastorakise4f0d3c2014-10-29 13:20:03 -0700325 // Ptr<Limits> limits = face->GetObject<Limits> ();
326 /*if (limits == 0)
327 {
328 NS_FATAL_ERROR ("Limits are enabled, but the selected forwarding strategy does not support
329 limits. Please revise your scenario");
330 exit (1);
331 }*/
Alexander Afanasyev122f3782013-02-02 00:04:40 -0800332
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800333 NS_LOG_INFO("Limits are enabled");
334 Ptr<PointToPointNetDevice> p2p = DynamicCast<PointToPointNetDevice>(device);
335 if (p2p != 0) {
336 // Setup bucket filtering
337 // Assume that we know average data packet size, and this size is equal default size
338 // Set maximum buckets (averaging over 1 second)
339
340 DataRateValue dataRate;
341 device->GetAttribute("DataRate", dataRate);
342 TimeValue linkDelay;
343 device->GetChannel()->GetAttribute("Delay", linkDelay);
344
345 NS_LOG_INFO("DataRate for this link is " << dataRate.Get());
346
347 double maxInterestPackets =
348 1.0 * dataRate.Get().GetBitRate() / 8.0 / (m_avgDataSize + m_avgInterestSize);
349 // NS_LOG_INFO ("Max packets per second: " << maxInterestPackets);
350 // NS_LOG_INFO ("Max burst: " << m_avgRtt.ToDouble (Time::S) * maxInterestPackets);
Spyridon Mastorakise4f0d3c2014-10-29 13:20:03 -0700351 // NS_LOG_INFO ("MaxLimit: " << (int)(m_avgRtt.ToDouble (Time::S) *
352 // maxInterestPackets));
353 /*
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800354 // Set max to BDP
Spyridon Mastorakise4f0d3c2014-10-29 13:20:03 -0700355 limits->SetLimits (maxInterestPackets, m_avgRtt.ToDouble (Time::S));
356 limits->SetLinkDelay (linkDelay.Get ().ToDouble (Time::S)); */
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800357 }
358 }
359
Alexander Afanasyev122f3782013-02-02 00:04:40 -0800360 return face;
361}
362
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700363Ptr<FaceContainer>
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800364StackHelper::Install(const std::string& nodeName) const
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700365{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800366 Ptr<Node> node = Names::Find<Node>(nodeName);
367 return Install(node);
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700368}
369
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700370void
Spyridon Mastorakise4f0d3c2014-10-29 13:20:03 -0700371StackHelper::AddRoute(Ptr<Node> node, const std::string& prefix, shared_ptr<Face> face, int32_t metric)
Alexander Afanasyev52e9aa92011-11-15 20:23:20 -0800372{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800373 NS_LOG_LOGIC("[" << node->GetId() << "]$ route add " << prefix << " via " << *face << " metric "
374 << metric);
Alexander Afanasyev4a5c2c12011-12-12 18:50:57 -0800375
Spyridon Mastorakise4f0d3c2014-10-29 13:20:03 -0700376 // Ptr<Fib> fib = node->GetObject<Fib> ();
Alexander Afanasyev52e9aa92011-11-15 20:23:20 -0800377
Spyridon Mastorakise4f0d3c2014-10-29 13:20:03 -0700378 // NameValue prefixValue;
379 // prefixValue.DeserializeFromString (prefix, MakeNameChecker ());
380 ::ndn::Name name(prefix);
381 // fib->Add (name, face, metric);
Alexander Afanasyev52e9aa92011-11-15 20:23:20 -0800382}
383
384void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800385StackHelper::AddRoute(Ptr<Node> node, const std::string& prefix, uint32_t faceId, int32_t metric)
Alexander Afanasyev8bedcaf2012-07-20 15:30:44 -0700386{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800387 Ptr<L3Protocol> ndn = node->GetObject<L3Protocol>();
388 NS_ASSERT_MSG(ndn != 0, "Ndn stack should be installed on the node");
Alexander Afanasyev8bedcaf2012-07-20 15:30:44 -0700389
Spyridon Mastorakise4f0d3c2014-10-29 13:20:03 -0700390 shared_ptr<Face> face = ndn->GetFace(faceId);
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800391 NS_ASSERT_MSG(face != 0, "Face with ID [" << faceId << "] does not exist on node ["
392 << node->GetId() << "]");
Alexander Afanasyev8bedcaf2012-07-20 15:30:44 -0700393
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800394 AddRoute(node, prefix, face, metric);
Alexander Afanasyev8bedcaf2012-07-20 15:30:44 -0700395}
396
397void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800398StackHelper::AddRoute(const std::string& nodeName, const std::string& prefix, uint32_t faceId,
399 int32_t metric)
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700400{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800401 Ptr<Node> node = Names::Find<Node>(nodeName);
402 NS_ASSERT_MSG(node != 0, "Node [" << nodeName << "] does not exist");
Alexander Afanasyev122f3782013-02-02 00:04:40 -0800403
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800404 Ptr<L3Protocol> ndn = node->GetObject<L3Protocol>();
405 NS_ASSERT_MSG(ndn != 0, "Ndn stack should be installed on the node");
Alexander Afanasyev52e9aa92011-11-15 20:23:20 -0800406
Spyridon Mastorakise4f0d3c2014-10-29 13:20:03 -0700407 shared_ptr<Face> face = ndn->GetFace(faceId);
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800408 NS_ASSERT_MSG(face != 0, "Face with ID [" << faceId << "] does not exist on node [" << nodeName
409 << "]");
Alexander Afanasyev52e9aa92011-11-15 20:23:20 -0800410
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800411 AddRoute(node, prefix, face, metric);
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700412}
Alexander Afanasyev4a5c2c12011-12-12 18:50:57 -0800413
Alexander Afanasyevfd258262012-11-23 14:59:19 -0800414void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800415StackHelper::AddRoute(Ptr<Node> node, const std::string& prefix, Ptr<Node> otherNode,
416 int32_t metric)
Alexander Afanasyevfd258262012-11-23 14:59:19 -0800417{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800418 for (uint32_t deviceId = 0; deviceId < node->GetNDevices(); deviceId++) {
419 Ptr<PointToPointNetDevice> netDevice =
420 DynamicCast<PointToPointNetDevice>(node->GetDevice(deviceId));
421 if (netDevice == 0)
422 continue;
Alexander Afanasyevfd258262012-11-23 14:59:19 -0800423
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800424 Ptr<Channel> channel = netDevice->GetChannel();
425 if (channel == 0)
426 continue;
Alexander Afanasyevfd258262012-11-23 14:59:19 -0800427
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800428 if (channel->GetDevice(0)->GetNode() == otherNode
429 || channel->GetDevice(1)->GetNode() == otherNode) {
430 Ptr<L3Protocol> ndn = node->GetObject<L3Protocol>();
431 NS_ASSERT_MSG(ndn != 0, "Ndn stack should be installed on the node");
Alexander Afanasyevfd258262012-11-23 14:59:19 -0800432
Spyridon Mastorakise4f0d3c2014-10-29 13:20:03 -0700433 shared_ptr<Face> face = ndn->GetFaceByNetDevice(netDevice);
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800434 NS_ASSERT_MSG(face != 0, "There is no face associated with the p2p link");
Alexander Afanasyevfd258262012-11-23 14:59:19 -0800435
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800436 AddRoute(node, prefix, face, metric);
Alexander Afanasyevfd258262012-11-23 14:59:19 -0800437
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800438 return;
Alexander Afanasyevfd258262012-11-23 14:59:19 -0800439 }
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800440 }
Alexander Afanasyevfd258262012-11-23 14:59:19 -0800441
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800442 NS_FATAL_ERROR("Cannot add route: Node# " << node->GetId() << " and Node# " << otherNode->GetId()
443 << " are not connected");
Alexander Afanasyevfd258262012-11-23 14:59:19 -0800444}
445
446void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800447StackHelper::AddRoute(const std::string& nodeName, const std::string& prefix,
448 const std::string& otherNodeName, int32_t metric)
Alexander Afanasyevfd258262012-11-23 14:59:19 -0800449{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800450 Ptr<Node> node = Names::Find<Node>(nodeName);
451 NS_ASSERT_MSG(node != 0, "Node [" << nodeName << "] does not exist");
Alexander Afanasyevfd258262012-11-23 14:59:19 -0800452
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800453 Ptr<Node> otherNode = Names::Find<Node>(otherNodeName);
454 NS_ASSERT_MSG(otherNode != 0, "Node [" << otherNodeName << "] does not exist");
Alexander Afanasyevfd258262012-11-23 14:59:19 -0800455
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800456 AddRoute(node, prefix, otherNode, metric);
Alexander Afanasyevfd258262012-11-23 14:59:19 -0800457}
458
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700459} // namespace ndn
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700460} // namespace ns3