blob: bc03c9daa6e02360e66cb9ebd9079732682cfd55 [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 Afanasyev11453142011-11-25 16:13:33 -080043#include "ns3/loopback-net-device.h"
Alexander Afanasyev52e9aa92011-11-15 20:23:20 -080044#include "ns3/global-router-interface.h"
Alexander Afanasyev11453142011-11-25 16:13:33 -080045#include "ns3/ipv4.h"
46#include "ns3/ipv4-global-routing.h"
Alexander Afanasyevf9f4eb02011-12-16 01:51:14 -080047#include "../utils/ipv4-global-routing-ordered-nexthops.h"
48
Alexander Afanasyev11453142011-11-25 16:13:33 -080049#include "ns3/ipv4-routing-helper.h"
Alexander Afanasyev52e9aa92011-11-15 20:23:20 -080050#include "ns3/ipv4-global-routing-helper.h"
Alexander Afanasyev11453142011-11-25 16:13:33 -080051#include "ns3/data-rate.h"
Alexander Afanasyev45b92d42011-08-14 23:11:38 -070052
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -070053#include "ccnx-face-container.h"
Alexander Afanasyev45b92d42011-08-14 23:11:38 -070054#include "ccnx-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 Afanasyev45b92d42011-08-14 23:11:38 -070061NS_LOG_COMPONENT_DEFINE ("CcnxStackHelper");
62
63namespace ns3 {
Ilya Moiseenko25f7d4d2011-09-29 18:41:06 -070064
Alexander Afanasyev45b92d42011-08-14 23:11:38 -070065CcnxStackHelper::CcnxStackHelper ()
Alexander Afanasyev4a5c2c12011-12-12 18:50:57 -080066 : m_limitsEnabled (false)
67 , m_needSetDefaultRoutes (false)
Alexander Afanasyev45b92d42011-08-14 23:11:38 -070068{
Alexander Afanasyev11453142011-11-25 16:13:33 -080069 m_strategyFactory.SetTypeId ("ns3::CcnxFloodingStrategy");
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 Afanasyev45b92d42011-08-14 23:11:38 -070076void
Alexander Afanasyev11453142011-11-25 16:13:33 -080077CcnxStackHelper::SetForwardingStrategy (std::string strategy)
Alexander Afanasyev45b92d42011-08-14 23:11:38 -070078{
Alexander Afanasyev11453142011-11-25 16:13:33 -080079 m_strategyFactory.SetTypeId (strategy);
80}
81
Alexander Afanasyev4a5c2c12011-12-12 18:50:57 -080082
83void
84CcnxStackHelper::SetDefaultRoutes (bool needSet)
85{
86 NS_LOG_FUNCTION (this << needSet);
87 m_needSetDefaultRoutes = needSet;
88}
89
Alexander Afanasyev11453142011-11-25 16:13:33 -080090void
Alexander Afanasyev33d62312012-01-09 13:50:20 -080091CcnxStackHelper::EnableLimits (bool enable/* = true*/,
92 Time avgRtt/*=Seconds(0.1)*/,
93 uint32_t avgContentObject/*=1100*/,
94 uint32_t avgInterest/*=40*/)
Alexander Afanasyev11453142011-11-25 16:13:33 -080095{
Alexander Afanasyevc39f0b42011-11-28 12:51:12 -080096 NS_LOG_INFO ("EnableLimits: " << enable);
Alexander Afanasyev11453142011-11-25 16:13:33 -080097 m_limitsEnabled = enable;
Alexander Afanasyev8f5a9bb2011-12-18 19:49:02 -080098 m_avgRtt = avgRtt;
Alexander Afanasyevc39f0b42011-11-28 12:51:12 -080099 m_avgContentObjectSize = avgContentObject;
100 m_avgInterestSize = avgInterest;
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700101}
102
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700103Ptr<CcnxFaceContainer>
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700104CcnxStackHelper::Install (NodeContainer c) const
105{
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700106 Ptr<CcnxFaceContainer> faces = Create<CcnxFaceContainer> ();
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700107 for (NodeContainer::Iterator i = c.Begin (); i != c.End (); ++i)
108 {
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700109 faces->AddAll (Install (*i));
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700110 }
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700111 return faces;
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::InstallAll (void) const
116{
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700117 return Install (NodeContainer::GetGlobal ());
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700118}
119
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700120Ptr<CcnxFaceContainer>
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700121CcnxStackHelper::Install (Ptr<Node> node) const
122{
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700123 // NS_ASSERT_MSG (m_forwarding, "SetForwardingHelper() should be set prior calling Install() method");
124 Ptr<CcnxFaceContainer> faces = Create<CcnxFaceContainer> ();
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700125
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700126 if (node->GetObject<Ccnx> () != 0)
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700127 {
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700128 NS_FATAL_ERROR ("CcnxStackHelper::Install (): Installing "
129 "a CcnxStack to a node with an existing Ccnx object");
130 return 0;
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700131 }
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700132
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700133 Ptr<CcnxFib> fib = CreateObject<CcnxFib> ();
134 node->AggregateObject (fib);
135
Ilya Moiseenkofbd0a8b2011-10-28 13:07:16 -0700136 Ptr<CcnxL3Protocol> ccnx = CreateObject<CcnxL3Protocol> ();
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700137 node->AggregateObject (ccnx);
138
Alexander Afanasyevf377b332011-12-16 15:32:12 -0800139 ccnx->SetForwardingStrategy (m_strategyFactory.Create<CcnxForwardingStrategy> ());
Alexander Afanasyev11453142011-11-25 16:13:33 -0800140
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700141 for (uint32_t index=0; index < node->GetNDevices (); index++)
142 {
Alexander Afanasyev11453142011-11-25 16:13:33 -0800143 Ptr<NetDevice> device = node->GetDevice (index);
144 if (DynamicCast<LoopbackNetDevice> (device) != 0)
145 continue; // don't create face for a LoopbackNetDevice
146
Alexander Afanasyevcbe92ae2011-12-16 13:06:18 -0800147 Ptr<CcnxNetDeviceFace> face = CreateObject<CcnxNetDeviceFace> (node, device);
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800148
Alexander Afanasyev4a5c2c12011-12-12 18:50:57 -0800149 ccnx->AddFace (face);
150 NS_LOG_LOGIC ("Node " << node->GetId () << ": added CcnxNetDeviceFace as face #" << *face);
Alexander Afanasyev11453142011-11-25 16:13:33 -0800151
Alexander Afanasyev4a5c2c12011-12-12 18:50:57 -0800152 if (m_needSetDefaultRoutes)
153 {
154 // default route with lowest priority possible
155 AddRoute (node, "/", face, std::numeric_limits<int32_t>::max ());
156 }
157
Alexander Afanasyev11453142011-11-25 16:13:33 -0800158 if (m_limitsEnabled)
159 {
Alexander Afanasyevc39f0b42011-11-28 12:51:12 -0800160 NS_LOG_INFO ("Limits are enabled");
Alexander Afanasyev11453142011-11-25 16:13:33 -0800161 Ptr<PointToPointNetDevice> p2p = DynamicCast<PointToPointNetDevice> (device);
162 if (p2p == 0)
Alexander Afanasyevc39f0b42011-11-28 12:51:12 -0800163 {
164 NS_LOG_INFO ("Non p2p interface");
165 continue; // only PointToPointNetDevice supports limits
166 }
Alexander Afanasyev11453142011-11-25 16:13:33 -0800167
168 // Setup bucket filtering
169 // Assume that we know average data packet size, and this size is equal default size
170 // Set maximum buckets (averaging over 1 second)
Ilya Moiseenkoc9266042011-11-02 17:49:21 -0700171
Alexander Afanasyev11453142011-11-25 16:13:33 -0800172 DataRateValue dataRate; device->GetAttribute ("DataRate", dataRate);
173
174 NS_LOG_INFO("DataRate for this link is " << dataRate.Get());
Alexander Afanasyevc39f0b42011-11-28 12:51:12 -0800175
Alexander Afanasyev33d62312012-01-09 13:50:20 -0800176 double maxInterestPackets = 1.0 * dataRate.Get ().GetBitRate () / 8.0 / (m_avgContentObjectSize + m_avgInterestSize);
Alexander Afanasyev8f5a9bb2011-12-18 19:49:02 -0800177 NS_LOG_INFO ("Max packets per second: " << maxInterestPackets);
178 NS_LOG_INFO ("Max burst: " << m_avgRtt.ToDouble (Time::S) * maxInterestPackets);
Alexander Afanasyevc39f0b42011-11-28 12:51:12 -0800179
180 // Set bucket max to BDP
181 face->SetBucketMax (m_avgRtt.ToDouble (Time::S) * maxInterestPackets); // number of interests allowed
182 face->SetBucketLeak (maxInterestPackets);
Alexander Afanasyev11453142011-11-25 16:13:33 -0800183 }
Ilya Moiseenkoc9266042011-11-02 17:49:21 -0700184
Ilya Moiseenko25f7d4d2011-09-29 18:41:06 -0700185 face->SetUp ();
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700186 faces->Add (face);
187 }
Ilya Moiseenko25f7d4d2011-09-29 18:41:06 -0700188
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700189 return faces;
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700190}
191
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700192Ptr<CcnxFaceContainer>
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700193CcnxStackHelper::Install (std::string nodeName) const
194{
195 Ptr<Node> node = Names::Find<Node> (nodeName);
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700196 return Install (node);
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700197}
198
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700199
200void
Alexander Afanasyevb7626842012-01-12 13:43:33 -0800201CcnxStackHelper::AddRoute (Ptr<Node> node, std::string prefix, Ptr<CcnxFace> face, int32_t metric)
Alexander Afanasyev52e9aa92011-11-15 20:23:20 -0800202{
Alexander Afanasyev4a5c2c12011-12-12 18:50:57 -0800203 NS_LOG_LOGIC ("[" << node->GetId () << "]$ route add " << prefix << " via " << *face << " metric " << metric);
204
Alexander Afanasyev52e9aa92011-11-15 20:23:20 -0800205 Ptr<CcnxFib> fib = node->GetObject<CcnxFib> ();
206
207 CcnxNameComponentsValue prefixValue;
208 prefixValue.DeserializeFromString (prefix, MakeCcnxNameComponentsChecker ());
209 fib->Add (prefixValue.Get (), face, metric);
210}
211
212void
Alexander Afanasyevb7626842012-01-12 13:43:33 -0800213CcnxStackHelper::AddRoute (std::string nodeName, std::string prefix, uint32_t faceId, int32_t metric)
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700214{
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700215 Ptr<Node> node = Names::Find<Node> (nodeName);
216 NS_ASSERT_MSG (node != 0, "Node [" << nodeName << "] does not exist");
Alexander Afanasyeva4e3f852011-11-15 20:39:33 -0800217
Alexander Afanasyev52e9aa92011-11-15 20:23:20 -0800218 Ptr<Ccnx> ccnx = node->GetObject<Ccnx> ();
219 NS_ASSERT_MSG (ccnx != 0, "Ccnx stack should be installed on the node");
220
221 Ptr<CcnxFace> face = ccnx->GetFace (faceId);
222 NS_ASSERT_MSG (face != 0, "Face with ID [" << faceId << "] does not exist on node [" << nodeName << "]");
223
224 AddRoute (node, prefix, face, metric);
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700225}
Alexander Afanasyev4a5c2c12011-12-12 18:50:57 -0800226
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700227
Alexander Afanasyev52e9aa92011-11-15 20:23:20 -0800228void
Alexander Afanasyev36d5c2a2012-01-02 19:09:19 -0800229CcnxStackHelper::InstallFakeGlobalRoutesImpl ()
Alexander Afanasyev52e9aa92011-11-15 20:23:20 -0800230{
231 for (NodeList::Iterator node = NodeList::Begin ();
232 node != NodeList::End ();
233 node ++)
234 {
235 NS_ASSERT_MSG ((*node)->GetObject<Ipv4> () != 0,
236 "InternetStack should be installed on all nodes");
237
238 NS_ASSERT_MSG (Ipv4RoutingHelper::GetRouting<Ipv4GlobalRoutingOrderedNexthops>
239 (
240 (*node)->GetObject<Ipv4> ()->GetRoutingProtocol ()
Alexander Afanasyev11453142011-11-25 16:13:33 -0800241 ),
Alexander Afanasyev52e9aa92011-11-15 20:23:20 -0800242 "InternetStack should have Ipv4GlobalRoutingOrderedNexthops as routing protocol");
243 // Example:
244 //
245 // Ipv4GlobalRoutingHelper ipv4RoutingHelper ("ns3::Ipv4GlobalRoutingUnorderedNexthops");
246 // stack.SetRoutingHelper (ipv4RoutingHelper);
247 //
248
249 Ptr<GlobalRouter> globalRouter = (*node)->GetObject<GlobalRouter> ();
250 if (globalRouter == 0) continue;
251
252 globalRouter->InjectRoute (Ipv4Address((*node)->GetId ()), Ipv4Mask("255.255.255.255"));
253 }
Alexander Afanasyev36d5c2a2012-01-02 19:09:19 -0800254}
Alexander Afanasyev52e9aa92011-11-15 20:23:20 -0800255
Alexander Afanasyev36d5c2a2012-01-02 19:09:19 -0800256void
257CcnxStackHelper::InstallFakeGlobalRoutes ()
258{
259 InstallFakeGlobalRoutesImpl ();
Alexander Afanasyev52e9aa92011-11-15 20:23:20 -0800260 Ipv4GlobalRoutingHelper::PopulateAllPossibleRoutingTables ();
261}
262
263void
264CcnxStackHelper::InstallRouteTo (Ptr<Node> destNode)
265{
Alexander Afanasyevb7626842012-01-12 13:43:33 -0800266 InstallRouteTo (boost::lexical_cast<std::string> (destNode->GetId ()), destNode);
267}
Alexander Afanasyev52e9aa92011-11-15 20:23:20 -0800268
Alexander Afanasyevb7626842012-01-12 13:43:33 -0800269void
270CcnxStackHelper::InstallRouteTo (const std::string &prefix, Ptr<Node> destNode)
271{
Alexander Afanasyev52e9aa92011-11-15 20:23:20 -0800272 Ipv4Address destIpv4 = Ipv4Address(destNode->GetId ());
273
274 for (NodeList::Iterator node = NodeList::Begin ();
275 node != NodeList::End ();
276 node ++)
277 {
278 if (destNode == *node) continue;
279
280 Ptr<Ccnx> ccnx = (*node)->GetObject<Ccnx> ();
281 NS_ASSERT_MSG (ccnx != 0, "CCNx stack should be installed on all nodes");
282
283 Ptr<Ipv4> ipv4 = (*node)->GetObject<Ipv4> ();
284 NS_ASSERT_MSG (ipv4 != 0,
285 "InternetStack should be installed on all nodes");
286
287 Ptr<Ipv4GlobalRoutingOrderedNexthops> routing =
288 Ipv4RoutingHelper::GetRouting<Ipv4GlobalRoutingOrderedNexthops> (ipv4->GetRoutingProtocol ());
289 NS_ASSERT_MSG (routing != 0, "Ipv4GlobalRoutingOrderedNexthops should be used in InternetStack");
290
291 Ptr<Ipv4GlobalRoutingOrderedNexthops::EntryContainer>
292 routes = routing->Lookup (destIpv4);
293
294 NS_ASSERT_MSG (routes != 0, "Should not happen... Call the developer");
295
296 BOOST_FOREACH (const Ipv4RoutingTableEntry &entry, *routes)
297 {
298 Ptr<NetDevice> netDevice = ipv4->GetNetDevice (entry.GetInterface ());
299 NS_ASSERT_MSG (netDevice != 0, "Should never happen. Call the popos");
300
301 Ptr<CcnxFace> face = ccnx->GetFaceByNetDevice (netDevice);
302 NS_ASSERT_MSG (face != 0, "Definitely should never happen. Call the president");
303
Alexander Afanasyevb7626842012-01-12 13:43:33 -0800304 AddRoute (*node, prefix, face, entry.GetMetric ());
Alexander Afanasyev52e9aa92011-11-15 20:23:20 -0800305 }
306 }
307}
308
309void
310CcnxStackHelper::InstallRoutesToAll ()
311{
312 for (NodeList::Iterator node = NodeList::Begin ();
313 node != NodeList::End ();
314 node ++)
315 {
316 InstallRouteTo (*node);
317 }
318}
319
320
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700321} // namespace ns3