blob: 8fbbcceebd73ea9ca13243944e5124ff2bf89b2d [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"
42#include "ns3/ccnx-name-components.h"
Alexander Afanasyev07827182011-12-13 01:07:32 -080043
Alexander Afanasyev52e9aa92011-11-15 20:23:20 -080044#include "ns3/node-list.h"
Alexander Afanasyev57bcbc32012-06-01 01:46:24 -070045// #include "ns3/loopback-net-device.h"
Alexander Afanasyevf9f4eb02011-12-16 01:51:14 -080046
Alexander Afanasyev11453142011-11-25 16:13:33 -080047#include "ns3/data-rate.h"
Alexander Afanasyev45b92d42011-08-14 23:11:38 -070048
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -070049#include "ccnx-face-container.h"
Alexander Afanasyev45b92d42011-08-14 23:11:38 -070050#include "ccnx-stack-helper.h"
Alexander Afanasyev45b92d42011-08-14 23:11:38 -070051
52#include <limits>
53#include <map>
Alexander Afanasyev52e9aa92011-11-15 20:23:20 -080054#include <boost/foreach.hpp>
Alexander Afanasyevb7626842012-01-12 13:43:33 -080055#include <boost/lexical_cast.hpp>
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -080056
Alexander Afanasyev45b92d42011-08-14 23:11:38 -070057NS_LOG_COMPONENT_DEFINE ("CcnxStackHelper");
58
59namespace ns3 {
Ilya Moiseenko25f7d4d2011-09-29 18:41:06 -070060
Alexander Afanasyev45b92d42011-08-14 23:11:38 -070061CcnxStackHelper::CcnxStackHelper ()
Alexander Afanasyev4a5c2c12011-12-12 18:50:57 -080062 : m_limitsEnabled (false)
63 , m_needSetDefaultRoutes (false)
Alexander Afanasyev45b92d42011-08-14 23:11:38 -070064{
Alexander Afanasyev3a4a0b32012-06-28 14:14:22 -070065 m_ccnxFactory. SetTypeId ("ns3::CcnxL3Protocol");
66 m_strategyFactory. SetTypeId ("ns3::CcnxFloodingStrategy");
Alexander Afanasyevd9fecdd2012-06-08 16:22:24 -070067 m_contentStoreFactory.SetTypeId ("ns3::CcnxContentStoreLru");
Alexander Afanasyev11f7bb42012-07-09 17:06:30 -070068 m_fibFactory. SetTypeId ("ns3::CcnxFib");
Alexander Afanasyev3a4a0b32012-06-28 14:14:22 -070069 m_pitFactory. SetTypeId ("ns3::CcnxPit");
Ilya Moiseenko25f7d4d2011-09-29 18:41:06 -070070}
71
Alexander Afanasyev45b92d42011-08-14 23:11:38 -070072CcnxStackHelper::~CcnxStackHelper ()
73{
Alexander Afanasyev45b92d42011-08-14 23:11:38 -070074}
75
Alexander Afanasyev3a4a0b32012-06-28 14:14:22 -070076void
77CcnxStackHelper::SetCcnxAttributes (const std::string &attr1, const std::string &value1,
78 const std::string &attr2, const std::string &value2,
79 const std::string &attr3, const std::string &value3,
80 const std::string &attr4, const std::string &value4)
81{
82 if (attr1 != "")
83 m_ccnxFactory.Set (attr1, StringValue (value1));
84 if (attr2 != "")
85 m_ccnxFactory.Set (attr2, StringValue (value2));
86 if (attr3 != "")
87 m_ccnxFactory.Set (attr3, StringValue (value3));
88 if (attr4 != "")
89 m_ccnxFactory.Set (attr4, StringValue (value4));
90}
91
Alexander Afanasyev45b92d42011-08-14 23:11:38 -070092void
Alexander Afanasyev3a4a0b32012-06-28 14:14:22 -070093CcnxStackHelper::SetForwardingStrategy (const std::string &strategy,
94 const std::string &attr1, const std::string &value1,
95 const std::string &attr2, const std::string &value2,
96 const std::string &attr3, const std::string &value3,
97 const std::string &attr4, const std::string &value4)
Alexander Afanasyev45b92d42011-08-14 23:11:38 -070098{
Alexander Afanasyev11453142011-11-25 16:13:33 -080099 m_strategyFactory.SetTypeId (strategy);
Alexander Afanasyev3a4a0b32012-06-28 14:14:22 -0700100 if (attr1 != "")
101 m_strategyFactory.Set (attr1, StringValue (value1));
102 if (attr2 != "")
103 m_strategyFactory.Set (attr2, StringValue (value2));
104 if (attr3 != "")
105 m_strategyFactory.Set (attr3, StringValue (value3));
106 if (attr4 != "")
107 m_strategyFactory.Set (attr4, StringValue (value4));
Alexander Afanasyev11453142011-11-25 16:13:33 -0800108}
109
Alexander Afanasyevd9fecdd2012-06-08 16:22:24 -0700110void
Alexander Afanasyev3a4a0b32012-06-28 14:14:22 -0700111CcnxStackHelper::SetContentStore (const std::string &contentStore,
112 const std::string &attr1, const std::string &value1,
113 const std::string &attr2, const std::string &value2,
114 const std::string &attr3, const std::string &value3,
115 const std::string &attr4, const std::string &value4)
Alexander Afanasyevd9fecdd2012-06-08 16:22:24 -0700116{
117 m_contentStoreFactory.SetTypeId (contentStore);
Alexander Afanasyev3a4a0b32012-06-28 14:14:22 -0700118 if (attr1 != "")
119 m_contentStoreFactory.Set (attr1, StringValue (value1));
120 if (attr2 != "")
121 m_contentStoreFactory.Set (attr2, StringValue (value2));
122 if (attr3 != "")
123 m_contentStoreFactory.Set (attr3, StringValue (value3));
124 if (attr4 != "")
125 m_contentStoreFactory.Set (attr4, StringValue (value4));
126}
127
128void
129CcnxStackHelper::SetPit (const std::string &pitClass,
130 const std::string &attr1, const std::string &value1,
131 const std::string &attr2, const std::string &value2,
132 const std::string &attr3, const std::string &value3,
133 const std::string &attr4, const std::string &value4)
134{
135 m_pitFactory.SetTypeId (pitClass);
136 if (attr1 != "")
137 m_pitFactory.Set (attr1, StringValue (value1));
138 if (attr2 != "")
139 m_pitFactory.Set (attr2, StringValue (value2));
140 if (attr3 != "")
141 m_pitFactory.Set (attr3, StringValue (value3));
142 if (attr4 != "")
143 m_pitFactory.Set (attr4, StringValue (value4));
144}
145
146void
147CcnxStackHelper::SetFib (const std::string &fibClass,
148 const std::string &attr1, const std::string &value1,
149 const std::string &attr2, const std::string &value2,
150 const std::string &attr3, const std::string &value3,
151 const std::string &attr4, const std::string &value4)
152{
153 m_fibFactory.SetTypeId (fibClass);
154 if (attr1 != "")
155 m_fibFactory.Set (attr1, StringValue (value1));
156 if (attr2 != "")
157 m_fibFactory.Set (attr2, StringValue (value2));
158 if (attr3 != "")
159 m_fibFactory.Set (attr3, StringValue (value3));
160 if (attr4 != "")
161 m_fibFactory.Set (attr4, StringValue (value4));
Alexander Afanasyevd9fecdd2012-06-08 16:22:24 -0700162}
Alexander Afanasyev4a5c2c12011-12-12 18:50:57 -0800163
164void
165CcnxStackHelper::SetDefaultRoutes (bool needSet)
166{
167 NS_LOG_FUNCTION (this << needSet);
168 m_needSetDefaultRoutes = needSet;
169}
170
Alexander Afanasyev11453142011-11-25 16:13:33 -0800171void
Alexander Afanasyev33d62312012-01-09 13:50:20 -0800172CcnxStackHelper::EnableLimits (bool enable/* = true*/,
173 Time avgRtt/*=Seconds(0.1)*/,
174 uint32_t avgContentObject/*=1100*/,
175 uint32_t avgInterest/*=40*/)
Alexander Afanasyev11453142011-11-25 16:13:33 -0800176{
Alexander Afanasyevc39f0b42011-11-28 12:51:12 -0800177 NS_LOG_INFO ("EnableLimits: " << enable);
Alexander Afanasyev11453142011-11-25 16:13:33 -0800178 m_limitsEnabled = enable;
Alexander Afanasyev8f5a9bb2011-12-18 19:49:02 -0800179 m_avgRtt = avgRtt;
Alexander Afanasyevc39f0b42011-11-28 12:51:12 -0800180 m_avgContentObjectSize = avgContentObject;
181 m_avgInterestSize = avgInterest;
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700182}
183
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700184Ptr<CcnxFaceContainer>
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700185CcnxStackHelper::Install (NodeContainer c) const
186{
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700187 Ptr<CcnxFaceContainer> faces = Create<CcnxFaceContainer> ();
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700188 for (NodeContainer::Iterator i = c.Begin (); i != c.End (); ++i)
189 {
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700190 faces->AddAll (Install (*i));
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700191 }
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700192 return faces;
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700193}
194
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700195Ptr<CcnxFaceContainer>
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700196CcnxStackHelper::InstallAll (void) const
197{
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700198 return Install (NodeContainer::GetGlobal ());
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700199}
200
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700201Ptr<CcnxFaceContainer>
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700202CcnxStackHelper::Install (Ptr<Node> node) const
203{
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700204 // NS_ASSERT_MSG (m_forwarding, "SetForwardingHelper() should be set prior calling Install() method");
205 Ptr<CcnxFaceContainer> faces = Create<CcnxFaceContainer> ();
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700206
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700207 if (node->GetObject<Ccnx> () != 0)
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700208 {
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700209 NS_FATAL_ERROR ("CcnxStackHelper::Install (): Installing "
210 "a CcnxStack to a node with an existing Ccnx object");
211 return 0;
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700212 }
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700213
Alexander Afanasyev3a4a0b32012-06-28 14:14:22 -0700214 // Create CcnxL3Protocol
215 Ptr<Ccnx> ccnx = m_ccnxFactory.Create<Ccnx> ();
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700216
Alexander Afanasyev3a4a0b32012-06-28 14:14:22 -0700217 // Create and aggregate FIB
Alexander Afanasyev44bb6ea2012-07-09 08:44:41 -0700218 Ptr<CcnxFib> fib = m_fibFactory.Create<CcnxFib> ();
219 ccnx->AggregateObject (fib);
Alexander Afanasyev3a4a0b32012-06-28 14:14:22 -0700220
221 // Create and aggregate PIT
222 ccnx->AggregateObject (m_pitFactory.Create<CcnxPit> ());
223
224 // Create and aggregate forwarding strategy
225 ccnx->AggregateObject (m_strategyFactory.Create<CcnxForwardingStrategy> ());
226
227 // Create and aggregate content store
228 ccnx->AggregateObject (m_contentStoreFactory.Create<CcnxContentStore> ());
229
230 // Aggregate CcnxL3Protocol on node
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700231 node->AggregateObject (ccnx);
Alexander Afanasyev11453142011-11-25 16:13:33 -0800232
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700233 for (uint32_t index=0; index < node->GetNDevices (); index++)
234 {
Alexander Afanasyev11453142011-11-25 16:13:33 -0800235 Ptr<NetDevice> device = node->GetDevice (index);
Alexander Afanasyev57bcbc32012-06-01 01:46:24 -0700236 // This check does not make sense: LoopbackNetDevice is installed only if IP stack is installed,
237 // Normally, ndnSIM works without IP stack, so no reason to check
238 // if (DynamicCast<LoopbackNetDevice> (device) != 0)
239 // continue; // don't create face for a LoopbackNetDevice
Alexander Afanasyev11453142011-11-25 16:13:33 -0800240
Alexander Afanasyevcbe92ae2011-12-16 13:06:18 -0800241 Ptr<CcnxNetDeviceFace> face = CreateObject<CcnxNetDeviceFace> (node, device);
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800242
Alexander Afanasyev4a5c2c12011-12-12 18:50:57 -0800243 ccnx->AddFace (face);
244 NS_LOG_LOGIC ("Node " << node->GetId () << ": added CcnxNetDeviceFace as face #" << *face);
Alexander Afanasyev11453142011-11-25 16:13:33 -0800245
Alexander Afanasyev4a5c2c12011-12-12 18:50:57 -0800246 if (m_needSetDefaultRoutes)
247 {
248 // default route with lowest priority possible
Alexander Afanasyev463fe222012-05-31 13:46:51 -0700249 AddRoute (node, "/", face, std::numeric_limits<int32_t>::max ());
Alexander Afanasyev4a5c2c12011-12-12 18:50:57 -0800250 }
251
Alexander Afanasyev11453142011-11-25 16:13:33 -0800252 if (m_limitsEnabled)
253 {
Alexander Afanasyevc39f0b42011-11-28 12:51:12 -0800254 NS_LOG_INFO ("Limits are enabled");
Alexander Afanasyev11453142011-11-25 16:13:33 -0800255 Ptr<PointToPointNetDevice> p2p = DynamicCast<PointToPointNetDevice> (device);
Alexander Afanasyev3f1c8b02012-01-19 19:41:34 -0800256 if (p2p != 0)
Alexander Afanasyevc39f0b42011-11-28 12:51:12 -0800257 {
Alexander Afanasyev3f1c8b02012-01-19 19:41:34 -0800258 // Setup bucket filtering
259 // Assume that we know average data packet size, and this size is equal default size
260 // Set maximum buckets (averaging over 1 second)
Ilya Moiseenkoc9266042011-11-02 17:49:21 -0700261
Alexander Afanasyev3f1c8b02012-01-19 19:41:34 -0800262 DataRateValue dataRate; device->GetAttribute ("DataRate", dataRate);
Alexander Afanasyev11453142011-11-25 16:13:33 -0800263
Alexander Afanasyev3f1c8b02012-01-19 19:41:34 -0800264 NS_LOG_INFO("DataRate for this link is " << dataRate.Get());
Alexander Afanasyevc39f0b42011-11-28 12:51:12 -0800265
Alexander Afanasyev3f1c8b02012-01-19 19:41:34 -0800266 double maxInterestPackets = 1.0 * dataRate.Get ().GetBitRate () / 8.0 / (m_avgContentObjectSize + m_avgInterestSize);
267 NS_LOG_INFO ("Max packets per second: " << maxInterestPackets);
268 NS_LOG_INFO ("Max burst: " << m_avgRtt.ToDouble (Time::S) * maxInterestPackets);
Alexander Afanasyevc39f0b42011-11-28 12:51:12 -0800269
Alexander Afanasyev3f1c8b02012-01-19 19:41:34 -0800270 // Set bucket max to BDP
271 face->SetBucketMax (m_avgRtt.ToDouble (Time::S) * maxInterestPackets); // number of interests allowed
272 face->SetBucketLeak (maxInterestPackets);
273 }
Alexander Afanasyev11453142011-11-25 16:13:33 -0800274 }
Ilya Moiseenkoc9266042011-11-02 17:49:21 -0700275
Ilya Moiseenko25f7d4d2011-09-29 18:41:06 -0700276 face->SetUp ();
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700277 faces->Add (face);
278 }
Ilya Moiseenko25f7d4d2011-09-29 18:41:06 -0700279
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700280 return faces;
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700281}
282
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700283Ptr<CcnxFaceContainer>
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700284CcnxStackHelper::Install (std::string nodeName) const
285{
286 Ptr<Node> node = Names::Find<Node> (nodeName);
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700287 return Install (node);
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700288}
289
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700290
291void
Alexander Afanasyevb7626842012-01-12 13:43:33 -0800292CcnxStackHelper::AddRoute (Ptr<Node> node, std::string prefix, Ptr<CcnxFace> face, int32_t metric)
Alexander Afanasyev52e9aa92011-11-15 20:23:20 -0800293{
Alexander Afanasyev4a5c2c12011-12-12 18:50:57 -0800294 NS_LOG_LOGIC ("[" << node->GetId () << "]$ route add " << prefix << " via " << *face << " metric " << metric);
295
Alexander Afanasyev52e9aa92011-11-15 20:23:20 -0800296 Ptr<CcnxFib> fib = node->GetObject<CcnxFib> ();
297
298 CcnxNameComponentsValue prefixValue;
299 prefixValue.DeserializeFromString (prefix, MakeCcnxNameComponentsChecker ());
300 fib->Add (prefixValue.Get (), face, metric);
301}
302
303void
Alexander Afanasyevb7626842012-01-12 13:43:33 -0800304CcnxStackHelper::AddRoute (std::string nodeName, std::string prefix, uint32_t faceId, int32_t metric)
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700305{
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700306 Ptr<Node> node = Names::Find<Node> (nodeName);
307 NS_ASSERT_MSG (node != 0, "Node [" << nodeName << "] does not exist");
Alexander Afanasyeva4e3f852011-11-15 20:39:33 -0800308
Alexander Afanasyev52e9aa92011-11-15 20:23:20 -0800309 Ptr<Ccnx> ccnx = node->GetObject<Ccnx> ();
310 NS_ASSERT_MSG (ccnx != 0, "Ccnx stack should be installed on the node");
311
312 Ptr<CcnxFace> face = ccnx->GetFace (faceId);
313 NS_ASSERT_MSG (face != 0, "Face with ID [" << faceId << "] does not exist on node [" << nodeName << "]");
314
315 AddRoute (node, prefix, face, metric);
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700316}
Alexander Afanasyev4a5c2c12011-12-12 18:50:57 -0800317
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700318
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700319} // namespace ns3