blob: a40e39ad138bdcdb54b11a9a3c89d7a39fba1310 [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
37#include "../model/ccnx-forwarding-strategy.h"
Alexander Afanasyevf9f4eb02011-12-16 01:51:14 -080038#include "../model/ccnx-net-device-face.h"
39#include "../model/ccnx-l3-protocol.h"
40#include "../model/ccnx-fib.h"
Alexander Afanasyev07827182011-12-13 01:07:32 -080041
Alexander Afanasyev52e9aa92011-11-15 20:23:20 -080042#include "ns3/node-list.h"
Alexander Afanasyev57bcbc32012-06-01 01:46:24 -070043// #include "ns3/loopback-net-device.h"
Alexander Afanasyevf9f4eb02011-12-16 01:51:14 -080044
Alexander Afanasyev11453142011-11-25 16:13:33 -080045#include "ns3/data-rate.h"
Alexander Afanasyev45b92d42011-08-14 23:11:38 -070046
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -070047#include "ccnx-face-container.h"
Alexander Afanasyev45b92d42011-08-14 23:11:38 -070048#include "ccnx-stack-helper.h"
Alexander Afanasyev45b92d42011-08-14 23:11:38 -070049
50#include <limits>
51#include <map>
Alexander Afanasyev52e9aa92011-11-15 20:23:20 -080052#include <boost/foreach.hpp>
Alexander Afanasyevb7626842012-01-12 13:43:33 -080053#include <boost/lexical_cast.hpp>
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -080054
Alexander Afanasyev45b92d42011-08-14 23:11:38 -070055NS_LOG_COMPONENT_DEFINE ("CcnxStackHelper");
56
57namespace ns3 {
Ilya Moiseenko25f7d4d2011-09-29 18:41:06 -070058
Alexander Afanasyev45b92d42011-08-14 23:11:38 -070059CcnxStackHelper::CcnxStackHelper ()
Alexander Afanasyev4a5c2c12011-12-12 18:50:57 -080060 : m_limitsEnabled (false)
61 , m_needSetDefaultRoutes (false)
Alexander Afanasyev45b92d42011-08-14 23:11:38 -070062{
Alexander Afanasyev11453142011-11-25 16:13:33 -080063 m_strategyFactory.SetTypeId ("ns3::CcnxFloodingStrategy");
Ilya Moiseenko25f7d4d2011-09-29 18:41:06 -070064}
65
Alexander Afanasyev45b92d42011-08-14 23:11:38 -070066CcnxStackHelper::~CcnxStackHelper ()
67{
Alexander Afanasyev45b92d42011-08-14 23:11:38 -070068}
69
Alexander Afanasyev45b92d42011-08-14 23:11:38 -070070void
Alexander Afanasyev11453142011-11-25 16:13:33 -080071CcnxStackHelper::SetForwardingStrategy (std::string strategy)
Alexander Afanasyev45b92d42011-08-14 23:11:38 -070072{
Alexander Afanasyev11453142011-11-25 16:13:33 -080073 m_strategyFactory.SetTypeId (strategy);
74}
75
Alexander Afanasyev4a5c2c12011-12-12 18:50:57 -080076
77void
78CcnxStackHelper::SetDefaultRoutes (bool needSet)
79{
80 NS_LOG_FUNCTION (this << needSet);
81 m_needSetDefaultRoutes = needSet;
82}
83
Alexander Afanasyev11453142011-11-25 16:13:33 -080084void
Alexander Afanasyev33d62312012-01-09 13:50:20 -080085CcnxStackHelper::EnableLimits (bool enable/* = true*/,
86 Time avgRtt/*=Seconds(0.1)*/,
87 uint32_t avgContentObject/*=1100*/,
88 uint32_t avgInterest/*=40*/)
Alexander Afanasyev11453142011-11-25 16:13:33 -080089{
Alexander Afanasyevc39f0b42011-11-28 12:51:12 -080090 NS_LOG_INFO ("EnableLimits: " << enable);
Alexander Afanasyev11453142011-11-25 16:13:33 -080091 m_limitsEnabled = enable;
Alexander Afanasyev8f5a9bb2011-12-18 19:49:02 -080092 m_avgRtt = avgRtt;
Alexander Afanasyevc39f0b42011-11-28 12:51:12 -080093 m_avgContentObjectSize = avgContentObject;
94 m_avgInterestSize = avgInterest;
Alexander Afanasyev45b92d42011-08-14 23:11:38 -070095}
96
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -070097Ptr<CcnxFaceContainer>
Alexander Afanasyev45b92d42011-08-14 23:11:38 -070098CcnxStackHelper::Install (NodeContainer c) const
99{
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700100 Ptr<CcnxFaceContainer> faces = Create<CcnxFaceContainer> ();
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700101 for (NodeContainer::Iterator i = c.Begin (); i != c.End (); ++i)
102 {
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700103 faces->AddAll (Install (*i));
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700104 }
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700105 return faces;
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700106}
107
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700108Ptr<CcnxFaceContainer>
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700109CcnxStackHelper::InstallAll (void) const
110{
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700111 return Install (NodeContainer::GetGlobal ());
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700112}
113
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700114Ptr<CcnxFaceContainer>
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700115CcnxStackHelper::Install (Ptr<Node> node) const
116{
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700117 // NS_ASSERT_MSG (m_forwarding, "SetForwardingHelper() should be set prior calling Install() method");
118 Ptr<CcnxFaceContainer> faces = Create<CcnxFaceContainer> ();
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700119
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700120 if (node->GetObject<Ccnx> () != 0)
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700121 {
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700122 NS_FATAL_ERROR ("CcnxStackHelper::Install (): Installing "
123 "a CcnxStack to a node with an existing Ccnx object");
124 return 0;
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700125 }
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700126
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700127 Ptr<CcnxFib> fib = CreateObject<CcnxFib> ();
128 node->AggregateObject (fib);
129
Ilya Moiseenkofbd0a8b2011-10-28 13:07:16 -0700130 Ptr<CcnxL3Protocol> ccnx = CreateObject<CcnxL3Protocol> ();
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700131 node->AggregateObject (ccnx);
132
Alexander Afanasyevf377b332011-12-16 15:32:12 -0800133 ccnx->SetForwardingStrategy (m_strategyFactory.Create<CcnxForwardingStrategy> ());
Alexander Afanasyev11453142011-11-25 16:13:33 -0800134
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700135 for (uint32_t index=0; index < node->GetNDevices (); index++)
136 {
Alexander Afanasyev11453142011-11-25 16:13:33 -0800137 Ptr<NetDevice> device = node->GetDevice (index);
Alexander Afanasyev57bcbc32012-06-01 01:46:24 -0700138 // This check does not make sense: LoopbackNetDevice is installed only if IP stack is installed,
139 // Normally, ndnSIM works without IP stack, so no reason to check
140 // if (DynamicCast<LoopbackNetDevice> (device) != 0)
141 // continue; // don't create face for a LoopbackNetDevice
Alexander Afanasyev11453142011-11-25 16:13:33 -0800142
Alexander Afanasyevcbe92ae2011-12-16 13:06:18 -0800143 Ptr<CcnxNetDeviceFace> face = CreateObject<CcnxNetDeviceFace> (node, device);
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800144
Alexander Afanasyev4a5c2c12011-12-12 18:50:57 -0800145 ccnx->AddFace (face);
146 NS_LOG_LOGIC ("Node " << node->GetId () << ": added CcnxNetDeviceFace as face #" << *face);
Alexander Afanasyev11453142011-11-25 16:13:33 -0800147
Alexander Afanasyev4a5c2c12011-12-12 18:50:57 -0800148 if (m_needSetDefaultRoutes)
149 {
150 // default route with lowest priority possible
Alexander Afanasyev463fe222012-05-31 13:46:51 -0700151 AddRoute (node, "/", face, std::numeric_limits<int32_t>::max ());
Alexander Afanasyev4a5c2c12011-12-12 18:50:57 -0800152 }
153
Alexander Afanasyev11453142011-11-25 16:13:33 -0800154 if (m_limitsEnabled)
155 {
Alexander Afanasyevc39f0b42011-11-28 12:51:12 -0800156 NS_LOG_INFO ("Limits are enabled");
Alexander Afanasyev11453142011-11-25 16:13:33 -0800157 Ptr<PointToPointNetDevice> p2p = DynamicCast<PointToPointNetDevice> (device);
Alexander Afanasyev3f1c8b02012-01-19 19:41:34 -0800158 if (p2p != 0)
Alexander Afanasyevc39f0b42011-11-28 12:51:12 -0800159 {
Alexander Afanasyev3f1c8b02012-01-19 19:41:34 -0800160 // Setup bucket filtering
161 // Assume that we know average data packet size, and this size is equal default size
162 // Set maximum buckets (averaging over 1 second)
Ilya Moiseenkoc9266042011-11-02 17:49:21 -0700163
Alexander Afanasyev3f1c8b02012-01-19 19:41:34 -0800164 DataRateValue dataRate; device->GetAttribute ("DataRate", dataRate);
Alexander Afanasyev11453142011-11-25 16:13:33 -0800165
Alexander Afanasyev3f1c8b02012-01-19 19:41:34 -0800166 NS_LOG_INFO("DataRate for this link is " << dataRate.Get());
Alexander Afanasyevc39f0b42011-11-28 12:51:12 -0800167
Alexander Afanasyev3f1c8b02012-01-19 19:41:34 -0800168 double maxInterestPackets = 1.0 * dataRate.Get ().GetBitRate () / 8.0 / (m_avgContentObjectSize + m_avgInterestSize);
169 NS_LOG_INFO ("Max packets per second: " << maxInterestPackets);
170 NS_LOG_INFO ("Max burst: " << m_avgRtt.ToDouble (Time::S) * maxInterestPackets);
Alexander Afanasyevc39f0b42011-11-28 12:51:12 -0800171
Alexander Afanasyev3f1c8b02012-01-19 19:41:34 -0800172 // Set bucket max to BDP
173 face->SetBucketMax (m_avgRtt.ToDouble (Time::S) * maxInterestPackets); // number of interests allowed
174 face->SetBucketLeak (maxInterestPackets);
175 }
Alexander Afanasyev11453142011-11-25 16:13:33 -0800176 }
Ilya Moiseenkoc9266042011-11-02 17:49:21 -0700177
Ilya Moiseenko25f7d4d2011-09-29 18:41:06 -0700178 face->SetUp ();
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700179 faces->Add (face);
180 }
Ilya Moiseenko25f7d4d2011-09-29 18:41:06 -0700181
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700182 return faces;
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700183}
184
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700185Ptr<CcnxFaceContainer>
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700186CcnxStackHelper::Install (std::string nodeName) const
187{
188 Ptr<Node> node = Names::Find<Node> (nodeName);
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700189 return Install (node);
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700190}
191
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700192
193void
Alexander Afanasyevb7626842012-01-12 13:43:33 -0800194CcnxStackHelper::AddRoute (Ptr<Node> node, std::string prefix, Ptr<CcnxFace> face, int32_t metric)
Alexander Afanasyev52e9aa92011-11-15 20:23:20 -0800195{
Alexander Afanasyev4a5c2c12011-12-12 18:50:57 -0800196 NS_LOG_LOGIC ("[" << node->GetId () << "]$ route add " << prefix << " via " << *face << " metric " << metric);
197
Alexander Afanasyev52e9aa92011-11-15 20:23:20 -0800198 Ptr<CcnxFib> fib = node->GetObject<CcnxFib> ();
199
200 CcnxNameComponentsValue prefixValue;
201 prefixValue.DeserializeFromString (prefix, MakeCcnxNameComponentsChecker ());
202 fib->Add (prefixValue.Get (), face, metric);
203}
204
205void
Alexander Afanasyevb7626842012-01-12 13:43:33 -0800206CcnxStackHelper::AddRoute (std::string nodeName, std::string prefix, uint32_t faceId, int32_t metric)
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700207{
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700208 Ptr<Node> node = Names::Find<Node> (nodeName);
209 NS_ASSERT_MSG (node != 0, "Node [" << nodeName << "] does not exist");
Alexander Afanasyeva4e3f852011-11-15 20:39:33 -0800210
Alexander Afanasyev52e9aa92011-11-15 20:23:20 -0800211 Ptr<Ccnx> ccnx = node->GetObject<Ccnx> ();
212 NS_ASSERT_MSG (ccnx != 0, "Ccnx stack should be installed on the node");
213
214 Ptr<CcnxFace> face = ccnx->GetFace (faceId);
215 NS_ASSERT_MSG (face != 0, "Face with ID [" << faceId << "] does not exist on node [" << nodeName << "]");
216
217 AddRoute (node, prefix, face, metric);
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700218}
Alexander Afanasyev4a5c2c12011-12-12 18:50:57 -0800219
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700220
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700221} // namespace ns3