blob: 9ee9aac923e3a2d91ba2402a4f9da712c3e57189 [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>
Ilya Moiseenkofbd0a8b2011-10-28 13:07:16 -070019 * 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"
31#include "ns3/callback.h"
32#include "ns3/node.h"
33#include "ns3/core-config.h"
Alexander Afanasyev07827182011-12-13 01:07:32 -080034#include "ns3/point-to-point-net-device.h"
35#include "ns3/point-to-point-helper.h"
36
Alexander Afanasyevf9f4eb02011-12-16 01:51:14 -080037#include "../model/ccnx-net-device-face.h"
38#include "../model/ccnx-l3-protocol.h"
Alexander Afanasyev11f7bb42012-07-09 17:06:30 -070039
40#include "ns3/ccnx-forwarding-strategy.h"
41#include "ns3/ccnx-fib.h"
Alexander Afanasyeve3d126f2012-07-16 17:07:31 -070042#include "ns3/ccnx-pit.h"
Alexander Afanasyev11f7bb42012-07-09 17:06:30 -070043#include "ns3/ccnx-name-components.h"
Alexander Afanasyeve3d126f2012-07-16 17:07:31 -070044#include "ns3/ccnx-content-store.h"
Alexander Afanasyev07827182011-12-13 01:07:32 -080045
Alexander Afanasyev52e9aa92011-11-15 20:23:20 -080046#include "ns3/node-list.h"
Alexander Afanasyev57bcbc32012-06-01 01:46:24 -070047// #include "ns3/loopback-net-device.h"
Alexander Afanasyevf9f4eb02011-12-16 01:51:14 -080048
Alexander Afanasyev11453142011-11-25 16:13:33 -080049#include "ns3/data-rate.h"
Alexander Afanasyev45b92d42011-08-14 23:11:38 -070050
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -070051#include "ccnx-face-container.h"
Alexander Afanasyev45b92d42011-08-14 23:11:38 -070052#include "ccnx-stack-helper.h"
Alexander Afanasyev45b92d42011-08-14 23:11:38 -070053
54#include <limits>
55#include <map>
Alexander Afanasyev52e9aa92011-11-15 20:23:20 -080056#include <boost/foreach.hpp>
Alexander Afanasyevb7626842012-01-12 13:43:33 -080057#include <boost/lexical_cast.hpp>
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -080058
Alexander Afanasyev45b92d42011-08-14 23:11:38 -070059NS_LOG_COMPONENT_DEFINE ("CcnxStackHelper");
60
61namespace ns3 {
Ilya Moiseenko25f7d4d2011-09-29 18:41:06 -070062
Alexander Afanasyev45b92d42011-08-14 23:11:38 -070063CcnxStackHelper::CcnxStackHelper ()
Alexander Afanasyev4a5c2c12011-12-12 18:50:57 -080064 : m_limitsEnabled (false)
65 , m_needSetDefaultRoutes (false)
Alexander Afanasyev45b92d42011-08-14 23:11:38 -070066{
Alexander Afanasyev3a4a0b32012-06-28 14:14:22 -070067 m_ccnxFactory. SetTypeId ("ns3::CcnxL3Protocol");
68 m_strategyFactory. SetTypeId ("ns3::CcnxFloodingStrategy");
Alexander Afanasyevd9fecdd2012-06-08 16:22:24 -070069 m_contentStoreFactory.SetTypeId ("ns3::CcnxContentStoreLru");
Alexander Afanasyev11f7bb42012-07-09 17:06:30 -070070 m_fibFactory. SetTypeId ("ns3::CcnxFib");
Alexander Afanasyev3a4a0b32012-06-28 14:14:22 -070071 m_pitFactory. SetTypeId ("ns3::CcnxPit");
Ilya Moiseenko25f7d4d2011-09-29 18:41:06 -070072}
73
Alexander Afanasyev45b92d42011-08-14 23:11:38 -070074CcnxStackHelper::~CcnxStackHelper ()
75{
Alexander Afanasyev45b92d42011-08-14 23:11:38 -070076}
77
Alexander Afanasyev3a4a0b32012-06-28 14:14:22 -070078void
79CcnxStackHelper::SetCcnxAttributes (const std::string &attr1, const std::string &value1,
80 const std::string &attr2, const std::string &value2,
81 const std::string &attr3, const std::string &value3,
82 const std::string &attr4, const std::string &value4)
83{
84 if (attr1 != "")
85 m_ccnxFactory.Set (attr1, StringValue (value1));
86 if (attr2 != "")
87 m_ccnxFactory.Set (attr2, StringValue (value2));
88 if (attr3 != "")
89 m_ccnxFactory.Set (attr3, StringValue (value3));
90 if (attr4 != "")
91 m_ccnxFactory.Set (attr4, StringValue (value4));
92}
93
Alexander Afanasyev45b92d42011-08-14 23:11:38 -070094void
Alexander Afanasyev3a4a0b32012-06-28 14:14:22 -070095CcnxStackHelper::SetForwardingStrategy (const std::string &strategy,
96 const std::string &attr1, const std::string &value1,
97 const std::string &attr2, const std::string &value2,
98 const std::string &attr3, const std::string &value3,
99 const std::string &attr4, const std::string &value4)
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700100{
Alexander Afanasyev11453142011-11-25 16:13:33 -0800101 m_strategyFactory.SetTypeId (strategy);
Alexander Afanasyev3a4a0b32012-06-28 14:14:22 -0700102 if (attr1 != "")
103 m_strategyFactory.Set (attr1, StringValue (value1));
104 if (attr2 != "")
105 m_strategyFactory.Set (attr2, StringValue (value2));
106 if (attr3 != "")
107 m_strategyFactory.Set (attr3, StringValue (value3));
108 if (attr4 != "")
109 m_strategyFactory.Set (attr4, StringValue (value4));
Alexander Afanasyev11453142011-11-25 16:13:33 -0800110}
111
Alexander Afanasyevd9fecdd2012-06-08 16:22:24 -0700112void
Alexander Afanasyev3a4a0b32012-06-28 14:14:22 -0700113CcnxStackHelper::SetContentStore (const std::string &contentStore,
114 const std::string &attr1, const std::string &value1,
115 const std::string &attr2, const std::string &value2,
116 const std::string &attr3, const std::string &value3,
117 const std::string &attr4, const std::string &value4)
Alexander Afanasyevd9fecdd2012-06-08 16:22:24 -0700118{
119 m_contentStoreFactory.SetTypeId (contentStore);
Alexander Afanasyev3a4a0b32012-06-28 14:14:22 -0700120 if (attr1 != "")
121 m_contentStoreFactory.Set (attr1, StringValue (value1));
122 if (attr2 != "")
123 m_contentStoreFactory.Set (attr2, StringValue (value2));
124 if (attr3 != "")
125 m_contentStoreFactory.Set (attr3, StringValue (value3));
126 if (attr4 != "")
127 m_contentStoreFactory.Set (attr4, StringValue (value4));
128}
129
130void
131CcnxStackHelper::SetPit (const std::string &pitClass,
132 const std::string &attr1, const std::string &value1,
133 const std::string &attr2, const std::string &value2,
134 const std::string &attr3, const std::string &value3,
135 const std::string &attr4, const std::string &value4)
136{
137 m_pitFactory.SetTypeId (pitClass);
138 if (attr1 != "")
139 m_pitFactory.Set (attr1, StringValue (value1));
140 if (attr2 != "")
141 m_pitFactory.Set (attr2, StringValue (value2));
142 if (attr3 != "")
143 m_pitFactory.Set (attr3, StringValue (value3));
144 if (attr4 != "")
145 m_pitFactory.Set (attr4, StringValue (value4));
146}
147
148void
149CcnxStackHelper::SetFib (const std::string &fibClass,
150 const std::string &attr1, const std::string &value1,
151 const std::string &attr2, const std::string &value2,
152 const std::string &attr3, const std::string &value3,
153 const std::string &attr4, const std::string &value4)
154{
155 m_fibFactory.SetTypeId (fibClass);
156 if (attr1 != "")
157 m_fibFactory.Set (attr1, StringValue (value1));
158 if (attr2 != "")
159 m_fibFactory.Set (attr2, StringValue (value2));
160 if (attr3 != "")
161 m_fibFactory.Set (attr3, StringValue (value3));
162 if (attr4 != "")
163 m_fibFactory.Set (attr4, StringValue (value4));
Alexander Afanasyevd9fecdd2012-06-08 16:22:24 -0700164}
Alexander Afanasyev4a5c2c12011-12-12 18:50:57 -0800165
166void
167CcnxStackHelper::SetDefaultRoutes (bool needSet)
168{
169 NS_LOG_FUNCTION (this << needSet);
170 m_needSetDefaultRoutes = needSet;
171}
172
Alexander Afanasyev11453142011-11-25 16:13:33 -0800173void
Alexander Afanasyev33d62312012-01-09 13:50:20 -0800174CcnxStackHelper::EnableLimits (bool enable/* = true*/,
175 Time avgRtt/*=Seconds(0.1)*/,
176 uint32_t avgContentObject/*=1100*/,
177 uint32_t avgInterest/*=40*/)
Alexander Afanasyev11453142011-11-25 16:13:33 -0800178{
Alexander Afanasyevc39f0b42011-11-28 12:51:12 -0800179 NS_LOG_INFO ("EnableLimits: " << enable);
Alexander Afanasyev11453142011-11-25 16:13:33 -0800180 m_limitsEnabled = enable;
Alexander Afanasyev8f5a9bb2011-12-18 19:49:02 -0800181 m_avgRtt = avgRtt;
Alexander Afanasyevc39f0b42011-11-28 12:51:12 -0800182 m_avgContentObjectSize = avgContentObject;
183 m_avgInterestSize = avgInterest;
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700184}
185
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700186Ptr<CcnxFaceContainer>
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700187CcnxStackHelper::Install (NodeContainer c) const
188{
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700189 Ptr<CcnxFaceContainer> faces = Create<CcnxFaceContainer> ();
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700190 for (NodeContainer::Iterator i = c.Begin (); i != c.End (); ++i)
191 {
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700192 faces->AddAll (Install (*i));
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700193 }
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700194 return faces;
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700195}
196
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700197Ptr<CcnxFaceContainer>
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700198CcnxStackHelper::InstallAll (void) const
199{
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700200 return Install (NodeContainer::GetGlobal ());
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700201}
202
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700203Ptr<CcnxFaceContainer>
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700204CcnxStackHelper::Install (Ptr<Node> node) const
205{
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700206 // NS_ASSERT_MSG (m_forwarding, "SetForwardingHelper() should be set prior calling Install() method");
207 Ptr<CcnxFaceContainer> faces = Create<CcnxFaceContainer> ();
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700208
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700209 if (node->GetObject<Ccnx> () != 0)
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700210 {
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700211 NS_FATAL_ERROR ("CcnxStackHelper::Install (): Installing "
212 "a CcnxStack to a node with an existing Ccnx object");
213 return 0;
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700214 }
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700215
Alexander Afanasyev3a4a0b32012-06-28 14:14:22 -0700216 // Create CcnxL3Protocol
217 Ptr<Ccnx> ccnx = m_ccnxFactory.Create<Ccnx> ();
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700218
Alexander Afanasyev3a4a0b32012-06-28 14:14:22 -0700219 // Create and aggregate FIB
Alexander Afanasyev44bb6ea2012-07-09 08:44:41 -0700220 Ptr<CcnxFib> fib = m_fibFactory.Create<CcnxFib> ();
221 ccnx->AggregateObject (fib);
Alexander Afanasyev3a4a0b32012-06-28 14:14:22 -0700222
223 // Create and aggregate PIT
224 ccnx->AggregateObject (m_pitFactory.Create<CcnxPit> ());
225
226 // Create and aggregate forwarding strategy
227 ccnx->AggregateObject (m_strategyFactory.Create<CcnxForwardingStrategy> ());
228
229 // Create and aggregate content store
230 ccnx->AggregateObject (m_contentStoreFactory.Create<CcnxContentStore> ());
231
232 // Aggregate CcnxL3Protocol on node
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700233 node->AggregateObject (ccnx);
Alexander Afanasyev11453142011-11-25 16:13:33 -0800234
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700235 for (uint32_t index=0; index < node->GetNDevices (); index++)
236 {
Alexander Afanasyev11453142011-11-25 16:13:33 -0800237 Ptr<NetDevice> device = node->GetDevice (index);
Alexander Afanasyev57bcbc32012-06-01 01:46:24 -0700238 // This check does not make sense: LoopbackNetDevice is installed only if IP stack is installed,
239 // Normally, ndnSIM works without IP stack, so no reason to check
240 // if (DynamicCast<LoopbackNetDevice> (device) != 0)
241 // continue; // don't create face for a LoopbackNetDevice
Alexander Afanasyev11453142011-11-25 16:13:33 -0800242
Alexander Afanasyevcbe92ae2011-12-16 13:06:18 -0800243 Ptr<CcnxNetDeviceFace> face = CreateObject<CcnxNetDeviceFace> (node, device);
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800244
Alexander Afanasyev4a5c2c12011-12-12 18:50:57 -0800245 ccnx->AddFace (face);
246 NS_LOG_LOGIC ("Node " << node->GetId () << ": added CcnxNetDeviceFace as face #" << *face);
Alexander Afanasyev11453142011-11-25 16:13:33 -0800247
Alexander Afanasyev4a5c2c12011-12-12 18:50:57 -0800248 if (m_needSetDefaultRoutes)
249 {
250 // default route with lowest priority possible
Alexander Afanasyev463fe222012-05-31 13:46:51 -0700251 AddRoute (node, "/", face, std::numeric_limits<int32_t>::max ());
Alexander Afanasyev4a5c2c12011-12-12 18:50:57 -0800252 }
253
Alexander Afanasyev11453142011-11-25 16:13:33 -0800254 if (m_limitsEnabled)
255 {
Alexander Afanasyevc39f0b42011-11-28 12:51:12 -0800256 NS_LOG_INFO ("Limits are enabled");
Alexander Afanasyev11453142011-11-25 16:13:33 -0800257 Ptr<PointToPointNetDevice> p2p = DynamicCast<PointToPointNetDevice> (device);
Alexander Afanasyev3f1c8b02012-01-19 19:41:34 -0800258 if (p2p != 0)
Alexander Afanasyevc39f0b42011-11-28 12:51:12 -0800259 {
Alexander Afanasyev3f1c8b02012-01-19 19:41:34 -0800260 // Setup bucket filtering
261 // Assume that we know average data packet size, and this size is equal default size
262 // Set maximum buckets (averaging over 1 second)
Ilya Moiseenkoc9266042011-11-02 17:49:21 -0700263
Alexander Afanasyev3f1c8b02012-01-19 19:41:34 -0800264 DataRateValue dataRate; device->GetAttribute ("DataRate", dataRate);
Alexander Afanasyev11453142011-11-25 16:13:33 -0800265
Alexander Afanasyev3f1c8b02012-01-19 19:41:34 -0800266 NS_LOG_INFO("DataRate for this link is " << dataRate.Get());
Alexander Afanasyevc39f0b42011-11-28 12:51:12 -0800267
Alexander Afanasyev3f1c8b02012-01-19 19:41:34 -0800268 double maxInterestPackets = 1.0 * dataRate.Get ().GetBitRate () / 8.0 / (m_avgContentObjectSize + m_avgInterestSize);
269 NS_LOG_INFO ("Max packets per second: " << maxInterestPackets);
270 NS_LOG_INFO ("Max burst: " << m_avgRtt.ToDouble (Time::S) * maxInterestPackets);
Alexander Afanasyevc39f0b42011-11-28 12:51:12 -0800271
Alexander Afanasyev3f1c8b02012-01-19 19:41:34 -0800272 // Set bucket max to BDP
273 face->SetBucketMax (m_avgRtt.ToDouble (Time::S) * maxInterestPackets); // number of interests allowed
274 face->SetBucketLeak (maxInterestPackets);
275 }
Alexander Afanasyev11453142011-11-25 16:13:33 -0800276 }
Ilya Moiseenkoc9266042011-11-02 17:49:21 -0700277
Ilya Moiseenko25f7d4d2011-09-29 18:41:06 -0700278 face->SetUp ();
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700279 faces->Add (face);
280 }
Ilya Moiseenko25f7d4d2011-09-29 18:41:06 -0700281
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700282 return faces;
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700283}
284
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700285Ptr<CcnxFaceContainer>
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700286CcnxStackHelper::Install (std::string nodeName) const
287{
288 Ptr<Node> node = Names::Find<Node> (nodeName);
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700289 return Install (node);
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700290}
291
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700292
293void
Alexander Afanasyevb7626842012-01-12 13:43:33 -0800294CcnxStackHelper::AddRoute (Ptr<Node> node, std::string prefix, Ptr<CcnxFace> face, int32_t metric)
Alexander Afanasyev52e9aa92011-11-15 20:23:20 -0800295{
Alexander Afanasyev4a5c2c12011-12-12 18:50:57 -0800296 NS_LOG_LOGIC ("[" << node->GetId () << "]$ route add " << prefix << " via " << *face << " metric " << metric);
297
Alexander Afanasyev52e9aa92011-11-15 20:23:20 -0800298 Ptr<CcnxFib> fib = node->GetObject<CcnxFib> ();
299
300 CcnxNameComponentsValue prefixValue;
301 prefixValue.DeserializeFromString (prefix, MakeCcnxNameComponentsChecker ());
302 fib->Add (prefixValue.Get (), face, metric);
303}
304
305void
Alexander Afanasyevb7626842012-01-12 13:43:33 -0800306CcnxStackHelper::AddRoute (std::string nodeName, std::string prefix, uint32_t faceId, int32_t metric)
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700307{
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700308 Ptr<Node> node = Names::Find<Node> (nodeName);
309 NS_ASSERT_MSG (node != 0, "Node [" << nodeName << "] does not exist");
Alexander Afanasyeva4e3f852011-11-15 20:39:33 -0800310
Alexander Afanasyev52e9aa92011-11-15 20:23:20 -0800311 Ptr<Ccnx> ccnx = node->GetObject<Ccnx> ();
312 NS_ASSERT_MSG (ccnx != 0, "Ccnx stack should be installed on the node");
313
314 Ptr<CcnxFace> face = ccnx->GetFace (faceId);
315 NS_ASSERT_MSG (face != 0, "Face with ID [" << faceId << "] does not exist on node [" << nodeName << "]");
316
317 AddRoute (node, prefix, face, metric);
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700318}
Alexander Afanasyev4a5c2c12011-12-12 18:50:57 -0800319
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700320
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700321} // namespace ns3