blob: 085078b0142669968bfb6e73750cc22abb664926 [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 Afanasyev4aac5572012-08-09 10:49:55 -070039#include "../model/ndn-net-device-face.h"
40#include "../model/ndn-l3-protocol.h"
Alexander Afanasyev11f7bb42012-07-09 17:06:30 -070041
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070042#include "ns3/ndn-forwarding-strategy.h"
43#include "ns3/ndn-fib.h"
44#include "ns3/ndn-pit.h"
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -070045#include "ns3/ndn-name.h"
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070046#include "ns3/ndn-content-store.h"
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 Afanasyev4aac5572012-08-09 10:49:55 -070053#include "ndn-face-container.h"
54#include "ndn-stack-helper.h"
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 Afanasyev2b4c9472012-08-09 15:00:38 -070061NS_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 Afanasyev2b4c9472012-08-09 15:00:38 -070066StackHelper::StackHelper ()
Alexander Afanasyev4a5c2c12011-12-12 18:50:57 -080067 : m_limitsEnabled (false)
68 , m_needSetDefaultRoutes (false)
Alexander Afanasyev45b92d42011-08-14 23:11:38 -070069{
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070070 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
76 m_netDeviceCallbacks.push_back (std::make_pair (PointToPointNetDevice::GetTypeId (), MakeCallback (&StackHelper::PointToPointNetDeviceCallback, this)));
77 // default callback will be fired if non of others callbacks fit or did the job
Ilya Moiseenko25f7d4d2011-09-29 18:41:06 -070078}
Alexander Afanasyev122f3782013-02-02 00:04:40 -080079
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070080StackHelper::~StackHelper ()
Alexander Afanasyev45b92d42011-08-14 23:11:38 -070081{
Alexander Afanasyev45b92d42011-08-14 23:11:38 -070082}
83
Alexander Afanasyev3a4a0b32012-06-28 14:14:22 -070084void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070085StackHelper::SetStackAttributes (const std::string &attr1, const std::string &value1,
86 const std::string &attr2, const std::string &value2,
87 const std::string &attr3, const std::string &value3,
88 const std::string &attr4, const std::string &value4)
Alexander Afanasyev3a4a0b32012-06-28 14:14:22 -070089{
90 if (attr1 != "")
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070091 m_ndnFactory.Set (attr1, StringValue (value1));
Alexander Afanasyev3a4a0b32012-06-28 14:14:22 -070092 if (attr2 != "")
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070093 m_ndnFactory.Set (attr2, StringValue (value2));
Alexander Afanasyev3a4a0b32012-06-28 14:14:22 -070094 if (attr3 != "")
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070095 m_ndnFactory.Set (attr3, StringValue (value3));
Alexander Afanasyev3a4a0b32012-06-28 14:14:22 -070096 if (attr4 != "")
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070097 m_ndnFactory.Set (attr4, StringValue (value4));
Alexander Afanasyev3a4a0b32012-06-28 14:14:22 -070098}
99
Alexander Afanasyev122f3782013-02-02 00:04:40 -0800100void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700101StackHelper::SetForwardingStrategy (const std::string &strategy,
Alexander Afanasyevfd258262012-11-23 14:59:19 -0800102 const std::string &attr1, const std::string &value1,
103 const std::string &attr2, const std::string &value2,
104 const std::string &attr3, const std::string &value3,
105 const std::string &attr4, const std::string &value4)
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700106{
Alexander Afanasyev11453142011-11-25 16:13:33 -0800107 m_strategyFactory.SetTypeId (strategy);
Alexander Afanasyev3a4a0b32012-06-28 14:14:22 -0700108 if (attr1 != "")
109 m_strategyFactory.Set (attr1, StringValue (value1));
110 if (attr2 != "")
111 m_strategyFactory.Set (attr2, StringValue (value2));
112 if (attr3 != "")
113 m_strategyFactory.Set (attr3, StringValue (value3));
114 if (attr4 != "")
115 m_strategyFactory.Set (attr4, StringValue (value4));
Alexander Afanasyev11453142011-11-25 16:13:33 -0800116}
117
Alexander Afanasyevd9fecdd2012-06-08 16:22:24 -0700118void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700119StackHelper::SetContentStore (const std::string &contentStore,
Alexander Afanasyevfd258262012-11-23 14:59:19 -0800120 const std::string &attr1, const std::string &value1,
121 const std::string &attr2, const std::string &value2,
122 const std::string &attr3, const std::string &value3,
123 const std::string &attr4, const std::string &value4)
Alexander Afanasyevd9fecdd2012-06-08 16:22:24 -0700124{
125 m_contentStoreFactory.SetTypeId (contentStore);
Alexander Afanasyev3a4a0b32012-06-28 14:14:22 -0700126 if (attr1 != "")
127 m_contentStoreFactory.Set (attr1, StringValue (value1));
128 if (attr2 != "")
129 m_contentStoreFactory.Set (attr2, StringValue (value2));
130 if (attr3 != "")
131 m_contentStoreFactory.Set (attr3, StringValue (value3));
132 if (attr4 != "")
133 m_contentStoreFactory.Set (attr4, StringValue (value4));
134}
135
136void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700137StackHelper::SetPit (const std::string &pitClass,
Alexander Afanasyevfd258262012-11-23 14:59:19 -0800138 const std::string &attr1, const std::string &value1,
139 const std::string &attr2, const std::string &value2,
140 const std::string &attr3, const std::string &value3,
141 const std::string &attr4, const std::string &value4)
Alexander Afanasyev3a4a0b32012-06-28 14:14:22 -0700142{
143 m_pitFactory.SetTypeId (pitClass);
144 if (attr1 != "")
145 m_pitFactory.Set (attr1, StringValue (value1));
146 if (attr2 != "")
147 m_pitFactory.Set (attr2, StringValue (value2));
148 if (attr3 != "")
149 m_pitFactory.Set (attr3, StringValue (value3));
150 if (attr4 != "")
151 m_pitFactory.Set (attr4, StringValue (value4));
152}
153
154void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700155StackHelper::SetFib (const std::string &fibClass,
Alexander Afanasyevfd258262012-11-23 14:59:19 -0800156 const std::string &attr1, const std::string &value1,
157 const std::string &attr2, const std::string &value2,
158 const std::string &attr3, const std::string &value3,
159 const std::string &attr4, const std::string &value4)
Alexander Afanasyev3a4a0b32012-06-28 14:14:22 -0700160{
161 m_fibFactory.SetTypeId (fibClass);
162 if (attr1 != "")
163 m_fibFactory.Set (attr1, StringValue (value1));
164 if (attr2 != "")
165 m_fibFactory.Set (attr2, StringValue (value2));
166 if (attr3 != "")
167 m_fibFactory.Set (attr3, StringValue (value3));
168 if (attr4 != "")
169 m_fibFactory.Set (attr4, StringValue (value4));
Alexander Afanasyevd9fecdd2012-06-08 16:22:24 -0700170}
Alexander Afanasyev4a5c2c12011-12-12 18:50:57 -0800171
172void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700173StackHelper::SetDefaultRoutes (bool needSet)
Alexander Afanasyev4a5c2c12011-12-12 18:50:57 -0800174{
175 NS_LOG_FUNCTION (this << needSet);
176 m_needSetDefaultRoutes = needSet;
177}
178
Alexander Afanasyev11453142011-11-25 16:13:33 -0800179void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700180StackHelper::EnableLimits (bool enable/* = true*/,
Alexander Afanasyevfd258262012-11-23 14:59:19 -0800181 Time avgRtt/*=Seconds(0.1)*/,
Alexander Afanasyev772f51b2013-08-01 18:53:25 -0700182 uint32_t avgData/*=1100*/,
Alexander Afanasyevfd258262012-11-23 14:59:19 -0800183 uint32_t avgInterest/*=40*/)
Alexander Afanasyev11453142011-11-25 16:13:33 -0800184{
Alexander Afanasyevc39f0b42011-11-28 12:51:12 -0800185 NS_LOG_INFO ("EnableLimits: " << enable);
Alexander Afanasyev11453142011-11-25 16:13:33 -0800186 m_limitsEnabled = enable;
Alexander Afanasyev8f5a9bb2011-12-18 19:49:02 -0800187 m_avgRtt = avgRtt;
Alexander Afanasyev772f51b2013-08-01 18:53:25 -0700188 m_avgDataSize = avgData;
Alexander Afanasyevc39f0b42011-11-28 12:51:12 -0800189 m_avgInterestSize = avgInterest;
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700190}
191
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700192Ptr<FaceContainer>
Alexander Afanasyevfd258262012-11-23 14:59:19 -0800193StackHelper::Install (const NodeContainer &c) const
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700194{
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700195 Ptr<FaceContainer> faces = Create<FaceContainer> ();
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700196 for (NodeContainer::Iterator i = c.Begin (); i != c.End (); ++i)
197 {
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700198 faces->AddAll (Install (*i));
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700199 }
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700200 return faces;
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700201}
202
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700203Ptr<FaceContainer>
Alexander Afanasyevfd258262012-11-23 14:59:19 -0800204StackHelper::InstallAll () const
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700205{
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700206 return Install (NodeContainer::GetGlobal ());
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700207}
208
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700209Ptr<FaceContainer>
210StackHelper::Install (Ptr<Node> node) const
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700211{
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700212 // NS_ASSERT_MSG (m_forwarding, "SetForwardingHelper() should be set prior calling Install() method");
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700213 Ptr<FaceContainer> faces = Create<FaceContainer> ();
Alexander Afanasyev122f3782013-02-02 00:04:40 -0800214
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700215 if (node->GetObject<L3Protocol> () != 0)
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700216 {
Alexander Afanasyev122f3782013-02-02 00:04:40 -0800217 NS_FATAL_ERROR ("StackHelper::Install (): Installing "
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700218 "a NdnStack to a node with an existing Ndn object");
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700219 return 0;
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700220 }
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700221
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700222 // Create L3Protocol
223 Ptr<L3Protocol> ndn = m_ndnFactory.Create<L3Protocol> ();
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700224
Alexander Afanasyev3a4a0b32012-06-28 14:14:22 -0700225 // Create and aggregate FIB
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700226 Ptr<Fib> fib = m_fibFactory.Create<Fib> ();
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700227 ndn->AggregateObject (fib);
Alexander Afanasyev3a4a0b32012-06-28 14:14:22 -0700228
229 // Create and aggregate PIT
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700230 ndn->AggregateObject (m_pitFactory.Create<Pit> ());
Alexander Afanasyev122f3782013-02-02 00:04:40 -0800231
Alexander Afanasyev3a4a0b32012-06-28 14:14:22 -0700232 // Create and aggregate forwarding strategy
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700233 ndn->AggregateObject (m_strategyFactory.Create<ForwardingStrategy> ());
Alexander Afanasyev3a4a0b32012-06-28 14:14:22 -0700234
235 // Create and aggregate content store
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700236 ndn->AggregateObject (m_contentStoreFactory.Create<ContentStore> ());
Alexander Afanasyev3a4a0b32012-06-28 14:14:22 -0700237
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700238 // Aggregate L3Protocol on node
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700239 node->AggregateObject (ndn);
Alexander Afanasyev122f3782013-02-02 00:04:40 -0800240
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700241 for (uint32_t index=0; index < node->GetNDevices (); index++)
242 {
Alexander Afanasyev11453142011-11-25 16:13:33 -0800243 Ptr<NetDevice> device = node->GetDevice (index);
Alexander Afanasyev57bcbc32012-06-01 01:46:24 -0700244 // This check does not make sense: LoopbackNetDevice is installed only if IP stack is installed,
245 // Normally, ndnSIM works without IP stack, so no reason to check
246 // if (DynamicCast<LoopbackNetDevice> (device) != 0)
247 // continue; // don't create face for a LoopbackNetDevice
Alexander Afanasyev11453142011-11-25 16:13:33 -0800248
Alexander Afanasyev122f3782013-02-02 00:04:40 -0800249 Ptr<NetDeviceFace> face;
250
251 for (std::list< std::pair<TypeId, NetDeviceFaceCreateCallback> >::const_iterator item = m_netDeviceCallbacks.begin ();
252 item != m_netDeviceCallbacks.end ();
253 item++)
254 {
255 if (device->GetInstanceTypeId () == item->first ||
256 device->GetInstanceTypeId ().IsChildOf (item->first))
257 {
Alexander Afanasyevc17e4bd2013-02-17 14:31:56 -0800258 face = item->second (node, ndn, device);
Alexander Afanasyev122f3782013-02-02 00:04:40 -0800259 if (face != 0)
260 break;
261 }
262 }
263 if (face == 0)
264 {
Alexander Afanasyevc17e4bd2013-02-17 14:31:56 -0800265 face = DefaultNetDeviceCallback (node, ndn, device);
Alexander Afanasyev122f3782013-02-02 00:04:40 -0800266 }
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800267
Alexander Afanasyev4a5c2c12011-12-12 18:50:57 -0800268 if (m_needSetDefaultRoutes)
269 {
270 // default route with lowest priority possible
Alexander Afanasyev122f3782013-02-02 00:04:40 -0800271 AddRoute (node, "/", StaticCast<Face> (face), std::numeric_limits<int32_t>::max ());
Alexander Afanasyev4a5c2c12011-12-12 18:50:57 -0800272 }
Alexander Afanasyevc39f0b42011-11-28 12:51:12 -0800273
Ilya Moiseenko25f7d4d2011-09-29 18:41:06 -0700274 face->SetUp ();
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700275 faces->Add (face);
276 }
Alexander Afanasyev122f3782013-02-02 00:04:40 -0800277
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700278 return faces;
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700279}
280
Alexander Afanasyev122f3782013-02-02 00:04:40 -0800281void
282StackHelper::AddNetDeviceFaceCreateCallback (TypeId netDeviceType, StackHelper::NetDeviceFaceCreateCallback callback)
283{
284 m_netDeviceCallbacks.push_back (std::make_pair (netDeviceType, callback));
285}
286
Alexander Afanasyev2a269f72013-06-06 22:59:33 -0700287void
288StackHelper::UpdateNetDeviceFaceCreateCallback (TypeId netDeviceType, NetDeviceFaceCreateCallback callback)
289{
290 for (NetDeviceCallbackList::iterator i = m_netDeviceCallbacks.begin (); i != m_netDeviceCallbacks.end (); i++)
291 {
292 if (i->first == netDeviceType)
293 {
294 i->second = callback;
295 return;
296 }
297 }
298}
299
300void
301StackHelper::RemoveNetDeviceFaceCreateCallback (TypeId netDeviceType, NetDeviceFaceCreateCallback callback)
302{
303 for (NetDeviceCallbackList::iterator i = m_netDeviceCallbacks.begin (); i != m_netDeviceCallbacks.end (); i++)
304 {
305 if (i->first == netDeviceType)
306 {
307 m_netDeviceCallbacks.erase (i);
308 return;
309 }
310 }
311}
Alexander Afanasyev122f3782013-02-02 00:04:40 -0800312
313Ptr<NetDeviceFace>
Alexander Afanasyevc17e4bd2013-02-17 14:31:56 -0800314StackHelper::DefaultNetDeviceCallback (Ptr<Node> node, Ptr<L3Protocol> ndn, Ptr<NetDevice> netDevice) const
Alexander Afanasyev122f3782013-02-02 00:04:40 -0800315{
316 NS_LOG_DEBUG ("Creating default NetDeviceFace on node " << node->GetId ());
317
Alexander Afanasyevc17e4bd2013-02-17 14:31:56 -0800318 Ptr<NetDeviceFace> face = CreateObject<NetDeviceFace> (node, netDevice);
319
320 ndn->AddFace (face);
321 NS_LOG_LOGIC ("Node " << node->GetId () << ": added NetDeviceFace as face #" << *face);
322
323 return face;
Alexander Afanasyev122f3782013-02-02 00:04:40 -0800324}
325
326Ptr<NetDeviceFace>
Alexander Afanasyevc17e4bd2013-02-17 14:31:56 -0800327StackHelper::PointToPointNetDeviceCallback (Ptr<Node> node, Ptr<L3Protocol> ndn, Ptr<NetDevice> device) const
Alexander Afanasyev122f3782013-02-02 00:04:40 -0800328{
329 NS_LOG_DEBUG ("Creating point-to-point NetDeviceFace on node " << node->GetId ());
330
331 Ptr<NetDeviceFace> face = CreateObject<NetDeviceFace> (node, device);
332
Alexander Afanasyevc17e4bd2013-02-17 14:31:56 -0800333 ndn->AddFace (face);
334 NS_LOG_LOGIC ("Node " << node->GetId () << ": added NetDeviceFace as face #" << *face);
335
Alexander Afanasyev122f3782013-02-02 00:04:40 -0800336 if (m_limitsEnabled)
337 {
338 Ptr<Limits> limits = face->GetObject<Limits> ();
339 if (limits == 0)
340 {
341 NS_FATAL_ERROR ("Limits are enabled, but the selected forwarding strategy does not support limits. Please revise your scenario");
342 exit (1);
343 }
344
345 NS_LOG_INFO ("Limits are enabled");
346 Ptr<PointToPointNetDevice> p2p = DynamicCast<PointToPointNetDevice> (device);
347 if (p2p != 0)
348 {
349 // Setup bucket filtering
350 // Assume that we know average data packet size, and this size is equal default size
351 // Set maximum buckets (averaging over 1 second)
352
353 DataRateValue dataRate; device->GetAttribute ("DataRate", dataRate);
354 TimeValue linkDelay; device->GetChannel ()->GetAttribute ("Delay", linkDelay);
355
356 NS_LOG_INFO("DataRate for this link is " << dataRate.Get());
357
Alexander Afanasyev772f51b2013-08-01 18:53:25 -0700358 double maxInterestPackets = 1.0 * dataRate.Get ().GetBitRate () / 8.0 / (m_avgDataSize + m_avgInterestSize);
Alexander Afanasyev122f3782013-02-02 00:04:40 -0800359 // NS_LOG_INFO ("Max packets per second: " << maxInterestPackets);
360 // NS_LOG_INFO ("Max burst: " << m_avgRtt.ToDouble (Time::S) * maxInterestPackets);
361 NS_LOG_INFO ("MaxLimit: " << (int)(m_avgRtt.ToDouble (Time::S) * maxInterestPackets));
362
363 // Set max to BDP
364 limits->SetLimits (maxInterestPackets, m_avgRtt.ToDouble (Time::S));
365 limits->SetLinkDelay (linkDelay.Get ().ToDouble (Time::S));
366 }
367 }
368
369 return face;
370}
371
372
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700373Ptr<FaceContainer>
Alexander Afanasyevfd258262012-11-23 14:59:19 -0800374StackHelper::Install (const std::string &nodeName) const
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700375{
376 Ptr<Node> node = Names::Find<Node> (nodeName);
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700377 return Install (node);
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700378}
379
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700380
381void
Alexander Afanasyevfd258262012-11-23 14:59:19 -0800382StackHelper::AddRoute (Ptr<Node> node, const std::string &prefix, Ptr<Face> face, int32_t metric)
Alexander Afanasyev52e9aa92011-11-15 20:23:20 -0800383{
Alexander Afanasyev4a5c2c12011-12-12 18:50:57 -0800384 NS_LOG_LOGIC ("[" << node->GetId () << "]$ route add " << prefix << " via " << *face << " metric " << metric);
385
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700386 Ptr<Fib> fib = node->GetObject<Fib> ();
Alexander Afanasyev52e9aa92011-11-15 20:23:20 -0800387
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -0700388 NameValue prefixValue;
389 prefixValue.DeserializeFromString (prefix, MakeNameChecker ());
Alexander Afanasyev52e9aa92011-11-15 20:23:20 -0800390 fib->Add (prefixValue.Get (), face, metric);
391}
392
393void
Alexander Afanasyevfd258262012-11-23 14:59:19 -0800394StackHelper::AddRoute (Ptr<Node> node, const std::string &prefix, uint32_t faceId, int32_t metric)
Alexander Afanasyev8bedcaf2012-07-20 15:30:44 -0700395{
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700396 Ptr<L3Protocol> ndn = node->GetObject<L3Protocol> ();
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700397 NS_ASSERT_MSG (ndn != 0, "Ndn stack should be installed on the node");
Alexander Afanasyev8bedcaf2012-07-20 15:30:44 -0700398
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700399 Ptr<Face> face = ndn->GetFace (faceId);
Alexander Afanasyev8bedcaf2012-07-20 15:30:44 -0700400 NS_ASSERT_MSG (face != 0, "Face with ID [" << faceId << "] does not exist on node [" << node->GetId () << "]");
401
402 AddRoute (node, prefix, face, metric);
403}
404
405void
Alexander Afanasyevfd258262012-11-23 14:59:19 -0800406StackHelper::AddRoute (const std::string &nodeName, const std::string &prefix, uint32_t faceId, int32_t metric)
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700407{
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700408 Ptr<Node> node = Names::Find<Node> (nodeName);
409 NS_ASSERT_MSG (node != 0, "Node [" << nodeName << "] does not exist");
Alexander Afanasyev122f3782013-02-02 00:04:40 -0800410
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700411 Ptr<L3Protocol> ndn = node->GetObject<L3Protocol> ();
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700412 NS_ASSERT_MSG (ndn != 0, "Ndn stack should be installed on the node");
Alexander Afanasyev52e9aa92011-11-15 20:23:20 -0800413
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700414 Ptr<Face> face = ndn->GetFace (faceId);
Alexander Afanasyev52e9aa92011-11-15 20:23:20 -0800415 NS_ASSERT_MSG (face != 0, "Face with ID [" << faceId << "] does not exist on node [" << nodeName << "]");
416
417 AddRoute (node, prefix, face, metric);
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700418}
Alexander Afanasyev4a5c2c12011-12-12 18:50:57 -0800419
Alexander Afanasyevfd258262012-11-23 14:59:19 -0800420void
421StackHelper::AddRoute (Ptr<Node> node, const std::string &prefix, Ptr<Node> otherNode, int32_t metric)
422{
423 for (uint32_t deviceId = 0; deviceId < node->GetNDevices (); deviceId ++)
424 {
425 Ptr<PointToPointNetDevice> netDevice = DynamicCast<PointToPointNetDevice> (node->GetDevice (deviceId));
426 if (netDevice == 0)
427 continue;
428
429 Ptr<Channel> channel = netDevice->GetChannel ();
430 if (channel == 0)
431 continue;
432
433 if (channel->GetDevice (0)->GetNode () == otherNode ||
434 channel->GetDevice (1)->GetNode () == otherNode)
435 {
436 Ptr<L3Protocol> ndn = node->GetObject<L3Protocol> ();
437 NS_ASSERT_MSG (ndn != 0, "Ndn stack should be installed on the node");
438
439 Ptr<Face> face = ndn->GetFaceByNetDevice (netDevice);
440 NS_ASSERT_MSG (face != 0, "There is no face associated with the p2p link");
441
442 AddRoute (node, prefix, face, metric);
443
444 return;
445 }
446 }
447
448 NS_FATAL_ERROR ("Cannot add route: Node# " << node->GetId () << " and Node# " << otherNode->GetId () << " are not connected");
449}
450
451void
452StackHelper::AddRoute (const std::string &nodeName, const std::string &prefix, const std::string &otherNodeName, int32_t metric)
453{
454 Ptr<Node> node = Names::Find<Node> (nodeName);
455 NS_ASSERT_MSG (node != 0, "Node [" << nodeName << "] does not exist");
456
457 Ptr<Node> otherNode = Names::Find<Node> (otherNodeName);
458 NS_ASSERT_MSG (otherNode != 0, "Node [" << otherNodeName << "] does not exist");
459
460 AddRoute (node, prefix, otherNode, metric);
461}
462
463
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700464} // namespace ndn
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700465} // namespace ns3