blob: 88f2344babcaf9db7351965f85e1f3e56a69d8d0 [file] [log] [blame]
Ilya Moiseenko46bdc7c2012-01-09 14:44:15 -08001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
2/*
3 * Copyright (c) 2011 University of California, Los Angeles
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 *
18 * Author: Ilya Moiseenko <iliamo@cs.ucla.edu>
19 */
20
21
22#include "ns3/core-module.h"
23#include "ns3/network-module.h"
24#include "ns3/point-to-point-module.h"
25#include "ns3/NDNabstraction-module.h"
26#include "ns3/point-to-point-grid.h"
27#include "ns3/ipv4-global-routing-helper.h"
28#include "ns3/random-variable.h"
Alexander Afanasyev6c678382012-01-22 16:15:41 -080029#include "ns3/ccnx.h"
Ilya Moiseenko2063c882012-01-11 19:59:32 -080030#include "ns3/topology-reader.h"
Alexander Afanasyev6c678382012-01-22 16:15:41 -080031#include "../model/ccnx-net-device-face.h"
Ilya Moiseenko46bdc7c2012-01-09 14:44:15 -080032
33#include <iostream>
34#include <sstream>
35#include <map>
36#include <list>
37#include <set>
38#include "ns3/rocketfuel-topology-reader.h"
39
40#include <boost/lexical_cast.hpp>
41#include <boost/foreach.hpp>
42
43using namespace ns3;
44using namespace std;
45using namespace boost;
46
47NS_LOG_COMPONENT_DEFINE ("LinkFailureSprint");
48
Alexander Afanasyev17acc112012-01-20 15:04:24 -080049#include "base-experiment.h"
50
51class Experiment : public BaseExperiment
Ilya Moiseenko46bdc7c2012-01-09 14:44:15 -080052{
53public:
Alexander Afanasyev6c678382012-01-22 16:15:41 -080054 typedef tuple<string, string> failure_t;
55 typedef list<failure_t> failures_t;
56
57 Experiment (const string &file)
58 {
59 ifstream failures (("./src/NDNabstraction/examples/failures/failures-"+file).c_str ());
60 for(std::string line; std::getline(failures, line); )
61 {
62 if (line == "")
63 {
64 m_failures.push_back (failures_t ());
65 continue;
66 }
67
68 failures_t failures;
69 istringstream run (line);
70 while (!run.eof () && !run.bad ())
71 {
72 int32_t link1 = -1;
73 int32_t link2 = -1;
74 run >> link1;
75 run.get ();
76 run >> link2;
77 run.get ();
78 if (link1 < 0 || link2 < 0) continue;
79
80 // cout << link1 << " <-> " << link2 << " ";
81 failures.push_back (failure_t (lexical_cast<string> (link1), lexical_cast<string> (link2)));
82 }
83 m_failures.push_back (failures);
84 }
85 }
86
Alexander Afanasyev17acc112012-01-20 15:04:24 -080087 // hijacker is more than an application. just disable all faces
Alexander Afanasyev6c678382012-01-22 16:15:41 -080088 void
Alexander Afanasyev17acc112012-01-20 15:04:24 -080089 FailLinks (uint32_t failId)
Ilya Moiseenko46bdc7c2012-01-09 14:44:15 -080090 {
Alexander Afanasyev6c678382012-01-22 16:15:41 -080091 failures_t failures = m_failures [failId];
92 BOOST_FOREACH (failure_t failure, failures)
93 {
94 Ptr<Node> node1 = Names::Find<Node> ("/sprint", failure.get<0> ());
95 Ptr<Node> node2 = Names::Find<Node> ("/sprint", failure.get<1> ());
96 // cout << failure.get<0> () << " <-> " << failure.get<1> () << " ";
97 // cout << node1 << ", " << node2 << "\n";
98
99 Ptr<Ccnx> ccnx1 = node1->GetObject<Ccnx> ();
100 Ptr<Ccnx> ccnx2 = node2->GetObject<Ccnx> ();
101 for (uint32_t faceId = 0; faceId < ccnx1->GetNFaces (); faceId++)
102 {
103 Ptr<CcnxFace> face = ccnx1->GetFace (faceId);
104 Ptr<CcnxNetDeviceFace> ndFace = face->GetObject<CcnxNetDeviceFace> ();
105 if (ndFace == 0) continue;
106
107 Ptr<PointToPointNetDevice> nd1 = ndFace->GetNetDevice ()->GetObject<PointToPointNetDevice> ();
108 if (nd1 == 0) continue;
109
110 Ptr<Channel> channel = nd1->GetChannel ();
111 if (channel == 0) continue;
112
113 Ptr<PointToPointChannel> ppChannel = DynamicCast<PointToPointChannel> (channel);
114
115 Ptr<NetDevice> nd2 = ppChannel->GetDevice (0);
116 if (nd2->GetNode () == node1)
117 nd2 = ppChannel->GetDevice (1);
118
119 if (Names::FindName (nd2->GetNode ()) == failure.get<1> ())
120 {
121 cout << "Failing " << failure.get<0> () << " <-> " << failure.get<1> () << " link\n";
122
123 Ptr<CcnxFace> face1 = ccnx1->GetFaceByNetDevice (nd1);
124 Ptr<CcnxFace> face2 = ccnx2->GetFaceByNetDevice (nd2);
125
126 face1->SetUp (false);
127 face2->SetUp (false);
Alexander Afanasyev1ec705f2012-01-23 12:35:10 -0800128
129 // set metric to max (for GlobalRouter to know what we want)
130 Ptr<Ipv4> ipv4_1 = ccnx1->GetObject<Ipv4> ();
131 Ptr<Ipv4> ipv4_2 = ccnx2->GetObject<Ipv4> ();
132
133 uint32_t if1 = ipv4_1->GetInterfaceForDevice (nd1);
134 uint32_t if2 = ipv4_2->GetInterfaceForDevice (nd2);
135
136 ipv4_1->SetMetric (if1, std::numeric_limits<uint16_t>::max ());
137 ipv4_2->SetMetric (if2, std::numeric_limits<uint16_t>::max ());
Alexander Afanasyev6c678382012-01-22 16:15:41 -0800138 break;
139 }
140 }
141 }
Alexander Afanasyev17acc112012-01-20 15:04:24 -0800142 }
Ilya Moiseenko46bdc7c2012-01-09 14:44:15 -0800143
Alexander Afanasyev17acc112012-01-20 15:04:24 -0800144 // void
145 // FailLinks(double threshold)
146 // {
147 // NS_LOG_INFO("Failing links");
148 // m_linkRand = UniformVariable(0, 1.0);
149 // double probability = 0.0;
Ilya Moiseenko46bdc7c2012-01-09 14:44:15 -0800150
Alexander Afanasyev17acc112012-01-20 15:04:24 -0800151 // BOOST_FOREACH (const TopologyReader::Link &link, m_reader.GetLinks())
152 // {
153 // probability = m_linkRand.GetValue();
154 // NS_LOG_INFO ("Probability = " << probability);
Ilya Moiseenko46bdc7c2012-01-09 14:44:15 -0800155
Alexander Afanasyev17acc112012-01-20 15:04:24 -0800156 // if(probability <= threshold)
157 // {
158 // Ptr<Node> fromNode = link.GetFromNode ();
159 // Ptr<Node> toNode = link.GetToNode ();
160 // NS_LOG_INFO("From node id = " << fromNode->GetId());
161 // NS_LOG_INFO("To node id = " << toNode->GetId());
Ilya Moiseenko2063c882012-01-11 19:59:32 -0800162
Alexander Afanasyev17acc112012-01-20 15:04:24 -0800163 // Ptr<CcnxL3Protocol> fromCcnx = fromNode->GetObject<CcnxL3Protocol> ();
164 // Ptr<CcnxL3Protocol> toCcnx = toNode->GetObject<CcnxL3Protocol> ();
Ilya Moiseenko2063c882012-01-11 19:59:32 -0800165
Alexander Afanasyev17acc112012-01-20 15:04:24 -0800166 // Ptr<NetDevice> fromDevice = link.GetFromNetDevice ();
167 // Ptr<NetDevice> toDevice = link.GetToNetDevice ();
Ilya Moiseenko2063c882012-01-11 19:59:32 -0800168
Alexander Afanasyev17acc112012-01-20 15:04:24 -0800169 // Ptr<CcnxFace> fromFace = fromCcnx->GetFaceByNetDevice (fromDevice);
170 // Ptr<CcnxFace> toFace = toCcnx->GetFaceByNetDevice (toDevice);
Ilya Moiseenko2063c882012-01-11 19:59:32 -0800171
Alexander Afanasyev17acc112012-01-20 15:04:24 -0800172 // NS_LOG_INFO("From face id = " << fromFace->GetId());
173 // NS_LOG_INFO("To face id = " << toFace->GetId());
174 // fromFace->SetUp (false);
175 // toFace->SetUp (false);
Ilya Moiseenko2063c882012-01-11 19:59:32 -0800176
Alexander Afanasyev17acc112012-01-20 15:04:24 -0800177 // NS_LOG_INFO(fromFace->IsUp());
178 // NS_LOG_INFO(toFace->IsUp());
179 // }
Ilya Moiseenko2063c882012-01-11 19:59:32 -0800180
Alexander Afanasyev17acc112012-01-20 15:04:24 -0800181 // }
182 // /*
183 // uint32_t nodeId = m_rand.GetValue ();
184 // Ptr<Node> node = Names::Find<Node> ("/sprint", lexical_cast<string> (nodeId));
Ilya Moiseenko2063c882012-01-11 19:59:32 -0800185
Alexander Afanasyev17acc112012-01-20 15:04:24 -0800186 // Ptr<CcnxL3Protocol> ccnx = node->GetObject<CcnxL3Protocol> ();
187 // UniformVariable faceRandom = UniformVariable (0, ccnx->GetNFaces ());
188 // uint32_t faceId = faceRandom.GetValue();
189 // Ptr<CcnxFace> face = ccnx->GetFace (faceId);
190 // face->SetUp(false);
191 // */
192 // }
Ilya Moiseenko2063c882012-01-11 19:59:32 -0800193
Ilya Moiseenko46bdc7c2012-01-09 14:44:15 -0800194 //We are creating "everybody-to-everybody" usage pattern
195 ApplicationContainer
196 AddApplications()
197 {
Alexander Afanasyev17acc112012-01-20 15:04:24 -0800198 NS_LOG_INFO ("Adding applications");
Alexander Afanasyev6c678382012-01-22 16:15:41 -0800199 NS_LOG_INFO ("GetN = " << reader->GetNodes().GetN());
200
201 double delay = 0;
Ilya Moiseenko2063c882012-01-11 19:59:32 -0800202
Ilya Moiseenko46bdc7c2012-01-09 14:44:15 -0800203 ApplicationContainer apps;
Alexander Afanasyev6c678382012-01-22 16:15:41 -0800204 for (uint32_t i = 0; i<reader->GetNodes().GetN(); i++)
Ilya Moiseenko46bdc7c2012-01-09 14:44:15 -0800205 {
Ilya Moiseenko2063c882012-01-11 19:59:32 -0800206 NS_LOG_INFO("i="<<i);
207 Ptr<Node> node1 = Names::Find<Node> ("/sprint", lexical_cast<string> (i));
208
Ilya Moiseenko46bdc7c2012-01-09 14:44:15 -0800209 CcnxAppHelper producerHelper ("ns3::CcnxProducer");
Ilya Moiseenko2063c882012-01-11 19:59:32 -0800210 producerHelper.SetPrefix ("/" + lexical_cast<string> (node1->GetId ()));
Ilya Moiseenko46bdc7c2012-01-09 14:44:15 -0800211
Alexander Afanasyev17acc112012-01-20 15:04:24 -0800212 apps.Add (producerHelper.Install (node1));
Ilya Moiseenko2063c882012-01-11 19:59:32 -0800213
Alexander Afanasyev17acc112012-01-20 15:04:24 -0800214 CcnxAppHelper consumerHelper ("ns3::CcnxConsumerBatches");
215 consumerHelper.SetAttribute ("LifeTime", StringValue("100s"));
Alexander Afanasyev6c678382012-01-22 16:15:41 -0800216 consumerHelper.SetAttribute ("Batches", StringValue("0s 1 1s 1 2s 1 3s 1 6s 1 20s 1"));
Alexander Afanasyev17acc112012-01-20 15:04:24 -0800217
Alexander Afanasyev6c678382012-01-22 16:15:41 -0800218 for(uint32_t j = 0; j<reader->GetNodes().GetN();j++)
Ilya Moiseenko46bdc7c2012-01-09 14:44:15 -0800219 {
Ilya Moiseenko2063c882012-01-11 19:59:32 -0800220 NS_LOG_INFO("j="<<j);
Ilya Moiseenko46bdc7c2012-01-09 14:44:15 -0800221 if(i==j)
222 continue;
223
Ilya Moiseenko2063c882012-01-11 19:59:32 -0800224 Ptr<Node> node2 = Names::Find<Node> ("/sprint", lexical_cast<string> (j));
225
Alexander Afanasyev17acc112012-01-20 15:04:24 -0800226 consumerHelper.SetPrefix ("/" + lexical_cast<string> (node1->GetId ()) + "/" + lexical_cast<string> (node2->GetId ()));
Alexander Afanasyev6c678382012-01-22 16:15:41 -0800227 ApplicationContainer consumer = consumerHelper.Install (node2);
228 consumer.Start (Seconds (delay));
229 apps.Add (consumer);
230
231 delay += 0.0001;
Ilya Moiseenko46bdc7c2012-01-09 14:44:15 -0800232 }
233 }
Ilya Moiseenko46bdc7c2012-01-09 14:44:15 -0800234
Ilya Moiseenko46bdc7c2012-01-09 14:44:15 -0800235 return apps;
236 }
Alexander Afanasyev6c678382012-01-22 16:15:41 -0800237
Alexander Afanasyev1ec705f2012-01-23 12:35:10 -0800238 void
239 RecalculateRouting ()
240 {
241 Ipv4GlobalRoutingHelper::PopulateRoutingTables ();
242 }
243
Alexander Afanasyev6c678382012-01-22 16:15:41 -0800244private:
245 vector<failures_t> m_failures;
Ilya Moiseenko46bdc7c2012-01-09 14:44:15 -0800246};
247
248int
249main (int argc, char *argv[])
250{
Ilya Moiseenko2063c882012-01-11 19:59:32 -0800251 cout << "Begin link failure scenario\n";
Ilya Moiseenko46bdc7c2012-01-09 14:44:15 -0800252
Alexander Afanasyev17acc112012-01-20 15:04:24 -0800253 Config::SetDefault ("ns3::PointToPointNetDevice::DataRate", StringValue ("100Mbps"));
254 Config::SetDefault ("ns3::DropTailQueue::MaxPackets", StringValue ("2000"));
255 Config::SetDefault ("ns3::RttEstimator::InitialEstimation", StringValue ("0.5s"));
Alexander Afanasyev1ec705f2012-01-23 12:35:10 -0800256 // Config::SetDefault ("ns3::RttEstimator::MaxMultiplier", StringValue ("16.0")); // original default is 64.0
Ilya Moiseenko46bdc7c2012-01-09 14:44:15 -0800257
Alexander Afanasyev17acc112012-01-20 15:04:24 -0800258 Config::SetDefault ("ns3::ConfigStore::Filename", StringValue ("attributes.xml"));
259 Config::SetDefault ("ns3::ConfigStore::Mode", StringValue ("Save"));
260 Config::SetDefault ("ns3::ConfigStore::FileFormat", StringValue ("Xml"));
261
Alexander Afanasyev1ec705f2012-01-23 12:35:10 -0800262 // Config::SetDefault ("ns3::CcnxConsumer::LifeTime", StringValue ("100s"));
263
Alexander Afanasyev17acc112012-01-20 15:04:24 -0800264 uint32_t maxRuns = 1;
265 uint32_t startRun = 0;
266 std::string failures = "";
Ilya Moiseenko46bdc7c2012-01-09 14:44:15 -0800267 CommandLine cmd;
Alexander Afanasyev17acc112012-01-20 15:04:24 -0800268 cmd.AddValue ("start", "Initial run number", startRun);
269 cmd.AddValue ("runs", "Number of runs", maxRuns);
270 cmd.AddValue ("failures", "File with failures", failures);
Ilya Moiseenko46bdc7c2012-01-09 14:44:15 -0800271 cmd.Parse (argc, argv);
272
Alexander Afanasyev17acc112012-01-20 15:04:24 -0800273 if (failures == "")
Ilya Moiseenko46bdc7c2012-01-09 14:44:15 -0800274 {
Alexander Afanasyev17acc112012-01-20 15:04:24 -0800275 std::cerr << "--failures=<file> parameter has to be specified" << std::endl;
276 return 1;
277 }
278
279 // ConfigStore config;
280 // config.ConfigureDefaults ();
281
Alexander Afanasyev6c678382012-01-22 16:15:41 -0800282 Experiment experiment (failures);
Alexander Afanasyev17acc112012-01-20 15:04:24 -0800283 for (uint32_t run = startRun; run < startRun + maxRuns; run++)
284 {
285 Config::SetGlobal ("RngRun", IntegerValue (run));
Ilya Moiseenko2063c882012-01-11 19:59:32 -0800286 cout << "seed = " << SeedManager::GetSeed () << ", run = " << SeedManager::GetRun () << endl;
287
Alexander Afanasyev17acc112012-01-20 15:04:24 -0800288 cout << "Run " << run << endl;
Alexander Afanasyev17acc112012-01-20 15:04:24 -0800289 string prefix = "link-failure-" + lexical_cast<string> (run) + "-";
Ilya Moiseenko46bdc7c2012-01-09 14:44:15 -0800290
291 experiment.ConfigureTopology ();
Alexander Afanasyev6c678382012-01-22 16:15:41 -0800292 experiment.InstallCcnxStack (true);
Ilya Moiseenko46bdc7c2012-01-09 14:44:15 -0800293 ApplicationContainer apps = experiment.AddApplications ();
Alexander Afanasyev6c678382012-01-22 16:15:41 -0800294 cout << "Total number of applications: " << apps.GetN () << "\n";
295
296 Simulator::Schedule (Seconds (10.0), &Experiment::FailLinks, &experiment, run);
Alexander Afanasyev1ec705f2012-01-23 12:35:10 -0800297 Simulator::Schedule (Seconds (39.0), &Experiment::RecalculateRouting, &experiment);
Alexander Afanasyev6c678382012-01-22 16:15:41 -0800298
Ilya Moiseenko2063c882012-01-11 19:59:32 -0800299 //tracing
300 CcnxTraceHelper traceHelper;
Alexander Afanasyev6c678382012-01-22 16:15:41 -0800301 Simulator::Schedule (Seconds (4.5), &CcnxTraceHelper::EnableSeqsAppAll, &traceHelper,
302 "ns3::CcnxConsumerBatches", prefix + "consumers-seqs.log");
303 Simulator::Schedule (Seconds (4.5), &CcnxTraceHelper::EnablePathWeights, &traceHelper,
304 prefix + "weights.log");
Ilya Moiseenko46bdc7c2012-01-09 14:44:15 -0800305
Alexander Afanasyev1ec705f2012-01-23 12:35:10 -0800306 experiment.Run (Seconds(60.0));
Alexander Afanasyev17acc112012-01-20 15:04:24 -0800307 }
308
Ilya Moiseenko2063c882012-01-11 19:59:32 -0800309 cout << "Finish link failure scenario\n";
Ilya Moiseenko46bdc7c2012-01-09 14:44:15 -0800310 return 0;
311}