Alexander Afanasyev | 812f7ab | 2012-01-24 11:08:04 -0800 | [diff] [blame] | 1 | /* -*- 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" |
| 29 | #include "ns3/ccnx.h" |
| 30 | #include "ns3/topology-reader.h" |
| 31 | #include "../model/ccnx-net-device-face.h" |
| 32 | |
| 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 | |
| 43 | using namespace ns3; |
| 44 | using namespace std; |
| 45 | using namespace boost; |
| 46 | |
| 47 | NS_LOG_COMPONENT_DEFINE ("LinkFailureSprint"); |
| 48 | |
| 49 | #include "base-experiment.h" |
| 50 | |
| 51 | class Experiment : public BaseExperiment |
| 52 | { |
| 53 | public: |
| 54 | 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 | |
| 87 | // hijacker is more than an application. just disable all faces |
| 88 | void |
| 89 | FailLinks (uint32_t failId) |
| 90 | { |
| 91 | 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); |
| 128 | |
| 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 ()); |
| 138 | break; |
| 139 | } |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | //We are creating "everybody-to-everybody" usage pattern |
| 145 | ApplicationContainer |
| 146 | AddApplications() |
| 147 | { |
| 148 | NS_LOG_INFO ("Adding applications"); |
| 149 | NS_LOG_INFO ("GetN = " << reader->GetNodes().GetN()); |
| 150 | |
| 151 | double delay = 0; |
| 152 | |
| 153 | ApplicationContainer apps; |
| 154 | for (uint32_t i = 0; i<reader->GetNodes().GetN(); i++) |
| 155 | { |
| 156 | NS_LOG_INFO("i="<<i); |
| 157 | Ptr<Node> node1 = Names::Find<Node> ("/sprint", lexical_cast<string> (i)); |
| 158 | |
| 159 | CcnxAppHelper producerHelper ("ns3::CcnxProducer"); |
| 160 | producerHelper.SetPrefix ("/" + lexical_cast<string> (node1->GetId ())); |
| 161 | |
| 162 | apps.Add (producerHelper.Install (node1)); |
| 163 | |
| 164 | CcnxAppHelper consumerHelper ("ns3::CcnxConsumerBatches"); |
| 165 | consumerHelper.SetAttribute ("LifeTime", StringValue("100s")); |
| 166 | consumerHelper.SetAttribute ("Batches", StringValue("2s 1")); |
| 167 | |
| 168 | for(uint32_t j = 0; j<reader->GetNodes().GetN();j++) |
| 169 | { |
| 170 | NS_LOG_INFO("j="<<j); |
| 171 | if(i==j) |
| 172 | continue; |
| 173 | |
| 174 | Ptr<Node> node2 = Names::Find<Node> ("/sprint", lexical_cast<string> (j)); |
| 175 | |
| 176 | consumerHelper.SetPrefix ("/" + lexical_cast<string> (node1->GetId ()) + "/" + lexical_cast<string> (node2->GetId ())); |
| 177 | ApplicationContainer consumer = consumerHelper.Install (node2); |
| 178 | consumer.Start (Seconds (delay)); |
| 179 | apps.Add (consumer); |
| 180 | |
| 181 | delay += 0.0001; |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | return apps; |
| 186 | } |
| 187 | |
| 188 | private: |
| 189 | vector<failures_t> m_failures; |
| 190 | }; |
| 191 | |
| 192 | int |
| 193 | main (int argc, char *argv[]) |
| 194 | { |
| 195 | cout << "Begin link failure scenario\n"; |
| 196 | |
| 197 | Config::SetDefault ("ns3::PointToPointNetDevice::DataRate", StringValue ("100Mbps")); |
| 198 | Config::SetDefault ("ns3::DropTailQueue::MaxPackets", StringValue ("2000")); |
| 199 | Config::SetDefault ("ns3::RttEstimator::InitialEstimation", StringValue ("0.5s")); |
| 200 | // Config::SetDefault ("ns3::RttEstimator::MaxMultiplier", StringValue ("16.0")); // original default is 64.0 |
| 201 | |
| 202 | Config::SetDefault ("ns3::ConfigStore::Filename", StringValue ("attributes.xml")); |
| 203 | Config::SetDefault ("ns3::ConfigStore::Mode", StringValue ("Save")); |
| 204 | Config::SetDefault ("ns3::ConfigStore::FileFormat", StringValue ("Xml")); |
| 205 | |
| 206 | // Config::SetDefault ("ns3::CcnxConsumer::LifeTime", StringValue ("100s")); |
| 207 | |
| 208 | uint32_t maxRuns = 1; |
| 209 | uint32_t startRun = 0; |
| 210 | std::string failures = ""; |
| 211 | CommandLine cmd; |
| 212 | cmd.AddValue ("start", "Initial run number", startRun); |
| 213 | cmd.AddValue ("runs", "Number of runs", maxRuns); |
| 214 | cmd.AddValue ("failures", "File with failures", failures); |
| 215 | cmd.Parse (argc, argv); |
| 216 | |
| 217 | if (failures == "") |
| 218 | { |
| 219 | std::cerr << "--failures=<file> parameter has to be specified" << std::endl; |
| 220 | return 1; |
| 221 | } |
| 222 | |
| 223 | // ConfigStore config; |
| 224 | // config.ConfigureDefaults (); |
| 225 | |
| 226 | Experiment experiment (failures); |
| 227 | for (uint32_t run = startRun; run < startRun + maxRuns; run++) |
| 228 | { |
| 229 | Config::SetGlobal ("RngRun", IntegerValue (run)); |
| 230 | cout << "seed = " << SeedManager::GetSeed () << ", run = " << SeedManager::GetRun () << endl; |
| 231 | |
| 232 | cout << "Run " << run << endl; |
| 233 | string prefix = "link-failure-"+ failures +"-base-" + lexical_cast<string> (run) + "-"; |
| 234 | |
| 235 | experiment.ConfigureTopology (); |
| 236 | experiment.InstallCcnxStackImpl (); |
| 237 | |
| 238 | CcnxStackHelper ccnxHelper; |
| 239 | ccnxHelper.InstallFakeGlobalRoutesImpl (); |
| 240 | |
| 241 | experiment.FailLinks (run); |
| 242 | |
| 243 | Ipv4GlobalRoutingHelper::PopulateRoutingTables (); |
| 244 | ccnxHelper.InstallRoutesToAll (); |
| 245 | |
| 246 | ApplicationContainer apps = experiment.AddApplications (); |
| 247 | cout << "Total number of applications: " << apps.GetN () << "\n"; |
| 248 | |
| 249 | //tracing |
| 250 | CcnxTraceHelper traceHelper; |
| 251 | // Simulator::Schedule (Seconds (4.5), &CcnxTraceHelper::EnableSeqsAppAll, &traceHelper, |
| 252 | // "ns3::CcnxConsumerBatches", prefix + "consumers-seqs.log"); |
| 253 | Simulator::Schedule (Seconds (0.1), &CcnxTraceHelper::EnablePathWeights, &traceHelper, |
| 254 | prefix + "weights.log"); |
| 255 | |
| 256 | experiment.Run (Seconds(10.0)); |
| 257 | } |
| 258 | |
| 259 | cout << "Finish link failure scenario\n"; |
| 260 | return 0; |
| 261 | } |