OLD | NEW |
(Empty) | |
| 1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 /* |
| 3 * Copyright (c) 2011 Yufei Cheng |
| 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: Yufei Cheng <yfcheng@ittc.ku.edu> |
| 19 * |
| 20 * James P.G. Sterbenz <jpgs@ittc.ku.edu>, director |
| 21 * ResiliNets Research Group http://wiki.ittc.ku.edu/resilinets |
| 22 * Information and Telecommunication Technology Center (ITTC) |
| 23 * and Department of Electrical Engineering and Computer Science |
| 24 * The University of Kansas Lawrence, KS USA. |
| 25 * |
| 26 * Work supported in part by NSF FIND (Future Internet Design) Program |
| 27 * under grant CNS-0626918 (Postmodern Internet Architecture), |
| 28 * NSF grant CNS-1050226 (Multilayer Network Resilience Analysis and Experimenta
tion on GENI), |
| 29 * US Department of Defense (DoD), and ITTC at The University of Kansas. |
| 30 * |
| 31 * Author of LEAR modification: Tomasz Seweryn <tomasz.seweryn7@gmail.com> |
| 32 */ |
| 33 |
| 34 #include "ns3/core-module.h" |
| 35 #include "ns3/network-module.h" |
| 36 #include "ns3/applications-module.h" |
| 37 #include "ns3/mobility-module.h" |
| 38 #include "ns3/config-store-module.h" |
| 39 #include "ns3/wifi-module.h" |
| 40 #include "ns3/internet-module.h" |
| 41 #include "ns3/dsr-module.h" |
| 42 #include <sstream> |
| 43 #include "ns3/energy-module.h" |
| 44 |
| 45 /* |
| 46 * Example simulation code for comparison of DSR and DSR-LEAR. |
| 47 * It generates *.pcap files with network flow for all network nodes. |
| 48 * These files can be used for further calculations of number of packets |
| 49 * (sent to)/(received by) the destination nodes and, as a result, become |
| 50 * basis for further calculations like PLR. Useful bash commands helping |
| 51 * in retrieving the number of unique packets delivered to destination nodes: |
| 52 * |
| 53 * tshark -R "data.len>1024 && ip.src==IP_SRC_ADDR && ip.dst==IP_DST_ADDR |
| 54 * && wlan.fc.retry != 1" -Tfields -e ip.id -r file_for_one_of_nodes.pcap | sort
| uniq | wc -l |
| 55 * |
| 56 * In the end of the simulation remaining energy and consumed energy are saved |
| 57 * to *.txt files. For metrics like standard deviation one has to parse |
| 58 * these files and perform calculations in bash script or another program. |
| 59 * |
| 60 * This simulation can be used to generate data, needed for calculation of |
| 61 * metrics for comparing DSR and DSR-LEAR the same way authors of this |
| 62 * energy model described it in their publication: |
| 63 * http://dl.acm.org/citation.cfm?id=882570 |
| 64 */ |
| 65 |
| 66 using namespace ns3; |
| 67 NS_LOG_COMPONENT_DEFINE("DsrTest"); |
| 68 |
| 69 //tested parameters |
| 70 uint32_t g_droppedPacketsCount = 0; |
| 71 uint32_t g_receivedPacketsCount = 0; |
| 72 uint32_t g_routingDroppedPacketsCount = 0; |
| 73 uint32_t g_routingReceivedPacketsCount = 0; |
| 74 |
| 75 void |
| 76 PhyRxDrop(Ptr<const Packet> p) |
| 77 { |
| 78 g_droppedPacketsCount++; |
| 79 } |
| 80 |
| 81 void |
| 82 PhyRxRecv(Ptr<const Packet> p) |
| 83 { |
| 84 g_receivedPacketsCount++; |
| 85 } |
| 86 |
| 87 void |
| 88 RoutingRxRecv(Ptr<const Packet> p) |
| 89 { |
| 90 g_routingReceivedPacketsCount++; |
| 91 } |
| 92 |
| 93 void |
| 94 RoutingRxDrop(Ptr<const Packet> p) |
| 95 { |
| 96 g_routingDroppedPacketsCount++; |
| 97 } |
| 98 |
| 99 void |
| 100 SaveNodesEnergyStats(uint32_t nWifis, uint32_t nSinks, double nodeSpeed, |
| 101 double pauseTime, double TotalTime, std::string rate, double dParam, |
| 102 EnergySourceContainer sources, NodeContainer adhocNodes, bool lear) |
| 103 { |
| 104 AsciiTraceHelper ascii; |
| 105 std::ostringstream o; |
| 106 if (lear) |
| 107 { |
| 108 o << "dsrLearDropRecvStats" << "_nW" << nWifis << "_nSi" << nSinks |
| 109 << "_nS" << nodeSpeed << "_pT" << pauseTime << "_tT" << TotalTime |
| 110 << "_" << rate << "_dP" << dParam << "_.txt"; |
| 111 } |
| 112 else |
| 113 { |
| 114 o << "dsrDropRecvStats" << "_nW" << nWifis << "_nSi" << nSinks << "_nS" |
| 115 << nodeSpeed << "_pT" << pauseTime << "_tT" << TotalTime << "_" |
| 116 << rate << "_dP" << dParam << "_.txt"; |
| 117 } |
| 118 |
| 119 Ptr<OutputStreamWrapper> stream = ascii.CreateFileStream(o.str()); |
| 120 // save RXDrop, RXRecv counters to the output file |
| 121 *stream->GetStream() << "PhyRxDropCntr,PhyRxRecvCntr," |
| 122 << g_droppedPacketsCount << "," << g_receivedPacketsCount << std::endl; |
| 123 *stream->GetStream() << "RoutingRxDropCntr,RoutingRxRecvCntr," |
| 124 << g_routingDroppedPacketsCount << "," << g_routingReceivedPacketsCount |
| 125 << std::endl; |
| 126 // save remaining energy stats and consumed energy stats |
| 127 std::ostringstream o1, o2; |
| 128 if (lear) |
| 129 { |
| 130 o1 << "dsr-lear-remainingEnergy" << "_nW" << nWifis << "_nSi" << nSinks |
| 131 << "_nS" << nodeSpeed << "_pT" << pauseTime << "_tT" << TotalTime |
| 132 << "_" << rate << "_dP" << dParam; |
| 133 o2 << "dsr-lear-consumedEnergy" << "_nW" << nWifis << "_nSi" << nSinks |
| 134 << "_nS" << nodeSpeed << "_pT" << pauseTime << "_tT" << TotalTime |
| 135 << "_" << rate << "_dP" << dParam; |
| 136 } |
| 137 else |
| 138 { |
| 139 o1 << "dsr-remainingEnergy" << "_nW" << nWifis << "_nSi" << nSinks |
| 140 << "_nS" << nodeSpeed << "_pT" << pauseTime << "_tT" << TotalTime |
| 141 << "_" << rate << "_dP" << dParam; |
| 142 o2 << "dsr-consumedEnergy" << "_nW" << nWifis << "_nSi" << nSinks << "_nS" |
| 143 << nodeSpeed << "_pT" << pauseTime << "_tT" << TotalTime << "_" |
| 144 << rate << "_dP" << dParam; |
| 145 } |
| 146 Ptr<OutputStreamWrapper> remainingEnergyFile = ascii.CreateFileStream( |
| 147 o1.str()); |
| 148 Ptr<OutputStreamWrapper> consumedEnergyFile = ascii.CreateFileStream( |
| 149 o2.str()); |
| 150 for (uint32_t i = 0; i < nWifis; i++) |
| 151 { |
| 152 std::ostringstream oss1, oss2, oss3; |
| 153 Ptr<BasicEnergySource> basicEnergySource; |
| 154 basicEnergySource = DynamicCast<BasicEnergySource>(sources.Get(i)); |
| 155 oss1 << adhocNodes.Get(i)->GetId() << " " |
| 156 << basicEnergySource->GetRemainingEnergy() << std::endl; |
| 157 *remainingEnergyFile->GetStream() << oss1.str(); |
| 158 // device energy model |
| 159 Ptr<DeviceEnergyModel> basicRadioModelPtr = |
| 160 basicEnergySource->FindDeviceEnergyModels("ns3::WifiRadioEnergyModel")
.Get( |
| 161 0); |
| 162 NS_ASSERT(basicRadioModelPtr != NULL); |
| 163 oss2 << adhocNodes.Get(i)->GetId() << " " |
| 164 << basicRadioModelPtr->GetTotalEnergyConsumption() << std::endl; |
| 165 *consumedEnergyFile->GetStream() << oss2.str(); |
| 166 } |
| 167 } |
| 168 |
| 169 int |
| 170 main(int argc, char *argv[]) |
| 171 { |
| 172 // |
| 173 // Users may find it convenient to turn on explicit debugging |
| 174 // for selected modules; the below lines suggest how to do this |
| 175 // |
| 176 #if 0 |
| 177 LogComponentEnable ("Ipv4L3Protocol", LOG_LEVEL_ALL); |
| 178 LogComponentEnable ("UdpL4Protocol", LOG_LEVEL_ALL); |
| 179 LogComponentEnable ("UdpSocketImpl", LOG_LEVEL_ALL); |
| 180 LogComponentEnable ("NetDevice", LOG_LEVEL_ALL); |
| 181 LogComponentEnable ("PacketSink", LOG_LEVEL_ALL); |
| 182 LogComponentEnable ("OnOffApplication", LOG_LEVEL_ALL); |
| 183 LogComponentEnable ("Ipv4EndPointDemux", LOG_LEVEL_ALL); |
| 184 #endif |
| 185 |
| 186 #if 0 |
| 187 LogComponentEnable ("DsrOptions", LOG_LEVEL_ALL); |
| 188 LogComponentEnable ("DsrHelper", LOG_LEVEL_ALL); |
| 189 LogComponentEnable ("DsrRouting", LOG_LEVEL_ALL); |
| 190 LogComponentEnable ("DsrOptionHeader", LOG_LEVEL_ALL); |
| 191 LogComponentEnable ("DsrFsHeader", LOG_LEVEL_ALL); |
| 192 LogComponentEnable ("DsrGraReplyTable", LOG_LEVEL_ALL); |
| 193 LogComponentEnable ("DsrSendBuffer", LOG_LEVEL_ALL); |
| 194 LogComponentEnable ("RouteCache", LOG_LEVEL_ALL); |
| 195 LogComponentEnable ("DsrMaintainBuffer", LOG_LEVEL_ALL); |
| 196 LogComponentEnable ("RreqTable", LOG_LEVEL_ALL); |
| 197 LogComponentEnable ("DsrErrorBuffer", LOG_LEVEL_ALL); |
| 198 LogComponentEnable ("DsrNetworkQueue", LOG_LEVEL_ALL); |
| 199 #endif |
| 200 |
| 201 NS_LOG_INFO("creating nodes"); |
| 202 |
| 203 // General parameters |
| 204 uint32_t nWifis = 40; |
| 205 uint32_t nSinks = 5; |
| 206 double TotalTime = 500.0; |
| 207 double dataTime = 500.0; |
| 208 double ppers = 1; |
| 209 uint32_t packetSize = 1024; |
| 210 double dataStart = 1.0; // start sending data at 1s |
| 211 bool lear = true; |
| 212 double initialEnergy = 10.0; |
| 213 double threshold = 9.9; |
| 214 double dParam = 0.1; |
| 215 |
| 216 //mobility parameters |
| 217 double pauseTime = 0.0; |
| 218 double nodeSpeed = 20.0; |
| 219 double txpDistance = 250.0; |
| 220 |
| 221 std::string rate = "40960bps"; |
| 222 std::string dataMode("DsssRate11Mbps"); |
| 223 std::string phyMode("DsssRate11Mbps"); |
| 224 |
| 225 //Allow users to override the default parameters and set it to new ones from C
ommandLine. |
| 226 CommandLine cmd; |
| 227 cmd.AddValue("nWifis", "Number of wifi nodes", nWifis); |
| 228 cmd.AddValue("nSinks", "Number of SINK traffic nodes", nSinks); |
| 229 cmd.AddValue("rate", "CBR traffic rate(in bps)", rate); |
| 230 cmd.AddValue("nodeSpeed", "Node speed in RandomWayPoint model", nodeSpeed); |
| 231 cmd.AddValue("packetSize", "The packet size", packetSize); |
| 232 cmd.AddValue("txpDistance", "Specify node's transmit range", txpDistance); |
| 233 cmd.AddValue("pauseTime", "pauseTime for mobility model", pauseTime); |
| 234 cmd.AddValue("lear", "Enable LEAR version of DSR", lear); |
| 235 cmd.AddValue("initialEnergy", "Initial energy for nodes", initialEnergy); |
| 236 cmd.AddValue("threshold", "Threshold parameter of LEAR algorithm", threshold); |
| 237 cmd.AddValue("dParam", "\"d\" parameter of LEAR algorithm", dParam); |
| 238 cmd.Parse(argc, argv); |
| 239 |
| 240 SeedManager::SetSeed(877); |
| 241 SeedManager::SetRun(1); |
| 242 |
| 243 NodeContainer adhocNodes; |
| 244 adhocNodes.Create(nWifis); |
| 245 NetDeviceContainer allDevices; |
| 246 |
| 247 NS_LOG_INFO("setting the default phy and channel parameters"); |
| 248 Config::SetDefault("ns3::WifiRemoteStationManager::NonUnicastMode", |
| 249 StringValue(phyMode)); |
| 250 Config::SetDefault("ns3::WifiRemoteStationManager::RtsCtsThreshold", |
| 251 StringValue("2200")); |
| 252 // disable fragmentation for frames below 2200 bytes |
| 253 Config::SetDefault("ns3::WifiRemoteStationManager::FragmentationThreshold", |
| 254 StringValue("2200")); |
| 255 |
| 256 WifiHelper wifi; |
| 257 wifi.SetStandard(WIFI_PHY_STANDARD_80211b); |
| 258 YansWifiPhyHelper wifiPhy = YansWifiPhyHelper::Default(); |
| 259 |
| 260 YansWifiChannelHelper wifiChannel; |
| 261 wifiChannel.SetPropagationDelay("ns3::ConstantSpeedPropagationDelayModel"); |
| 262 wifiChannel.AddPropagationLoss("ns3::RangePropagationLossModel", "MaxRange", |
| 263 DoubleValue(txpDistance)); |
| 264 wifiPhy.SetChannel(wifiChannel.Create()); |
| 265 |
| 266 // Add a non-QoS upper mac, and disable rate control |
| 267 NqosWifiMacHelper wifiMac = NqosWifiMacHelper::Default(); |
| 268 wifi.SetRemoteStationManager("ns3::ConstantRateWifiManager", "DataMode", |
| 269 StringValue(dataMode), "ControlMode", StringValue(phyMode)); |
| 270 |
| 271 wifiMac.SetType("ns3::AdhocWifiMac"); |
| 272 allDevices = wifi.Install(wifiPhy, wifiMac, adhocNodes); |
| 273 |
| 274 NS_LOG_INFO("Configure Tracing."); |
| 275 |
| 276 if (!lear) |
| 277 { |
| 278 wifiPhy.EnablePcapAll("dsr", false); |
| 279 } |
| 280 else |
| 281 { |
| 282 wifiPhy.EnablePcapAll("dsrlear", false); |
| 283 } |
| 284 |
| 285 MobilityHelper adhocMobility; |
| 286 ObjectFactory pos; |
| 287 pos.SetTypeId("ns3::RandomRectanglePositionAllocator"); |
| 288 pos.Set("X", StringValue("ns3::UniformRandomVariable[Min=0.0|Max=1000.0]")); |
| 289 pos.Set("Y", StringValue("ns3::UniformRandomVariable[Min=0.0|Max=1000.0]")); |
| 290 Ptr<PositionAllocator> taPositionAlloc = pos.Create()->GetObject< |
| 291 PositionAllocator>(); |
| 292 |
| 293 std::ostringstream speedUniformRandomVariableStream; |
| 294 speedUniformRandomVariableStream << "ns3::UniformRandomVariable[Min=0.0|Max=" |
| 295 << nodeSpeed << "]"; |
| 296 |
| 297 std::ostringstream pauseConstantRandomVariableStream; |
| 298 pauseConstantRandomVariableStream << "ns3::ConstantRandomVariable[Constant=" |
| 299 << pauseTime << "]"; |
| 300 |
| 301 adhocMobility.SetMobilityModel("ns3::RandomWaypointMobilityModel", |
| 302 // "Speed", StringValue ("ns3::UniformRan
domVariable[Min=0.0|Max=nodeSpeed]"), |
| 303 "Speed", StringValue(speedUniformRandomVariableStream.str()), "Pause", |
| 304 StringValue(pauseConstantRandomVariableStream.str()), "PositionAllocator", |
| 305 PointerValue(taPositionAlloc)); |
| 306 adhocMobility.Install(adhocNodes); |
| 307 |
| 308 /** Energy Model **/ |
| 309 /***************************************************************************/ |
| 310 /* energy source */ |
| 311 BasicEnergySourceHelper basicSourceHelper; |
| 312 // configure energy source |
| 313 basicSourceHelper.Set("BasicEnergySourceInitialEnergyJ", |
| 314 DoubleValue(initialEnergy)); |
| 315 // install source |
| 316 EnergySourceContainer sources = basicSourceHelper.Install(adhocNodes); |
| 317 /* device energy model */ |
| 318 WifiRadioEnergyModelHelper radioEnergyHelper; |
| 319 // install device model |
| 320 DeviceEnergyModelContainer deviceModels = radioEnergyHelper.Install( |
| 321 allDevices, sources); |
| 322 /***************************************************************************/ |
| 323 |
| 324 InternetStackHelper internet; |
| 325 DsrMainHelper dsrMain; |
| 326 DsrHelper dsr; |
| 327 internet.Install(adhocNodes); |
| 328 if (lear) |
| 329 { |
| 330 dsr.Set("Lear", BooleanValue(true)); |
| 331 dsr.Set("ThresholdJ", DoubleValue(threshold)); |
| 332 dsr.Set("DParamJ", DoubleValue(dParam)); |
| 333 dsr.Set("DropRequestIdSize", UintegerValue(160)); |
| 334 } |
| 335 dsrMain.Install(dsr, adhocNodes); |
| 336 |
| 337 NS_LOG_INFO("assigning ip address"); |
| 338 Ipv4AddressHelper address; |
| 339 address.SetBase("10.1.1.0", "255.255.255.0"); |
| 340 Ipv4InterfaceContainer allInterfaces; |
| 341 allInterfaces = address.Assign(allDevices); |
| 342 |
| 343 uint16_t port = 9; |
| 344 double randomStartTime = (1 / ppers) / nSinks; //distributed btw 1s evenly as
we are sending 1pkt/s |
| 345 |
| 346 for (uint32_t i = 0; i < nSinks; ++i) |
| 347 { |
| 348 PacketSinkHelper sink("ns3::UdpSocketFactory", |
| 349 InetSocketAddress(Ipv4Address::GetAny(), port)); |
| 350 ApplicationContainer apps_sink = sink.Install(adhocNodes.Get(i)); |
| 351 apps_sink.Start(Seconds(0.0)); |
| 352 apps_sink.Stop(Seconds(TotalTime)); |
| 353 |
| 354 OnOffHelper onoff1("ns3::UdpSocketFactory", |
| 355 Address(InetSocketAddress(allInterfaces.GetAddress(i), port))); |
| 356 onoff1.SetAttribute("OnTime", |
| 357 StringValue("ns3::ConstantRandomVariable[Constant=1.0]")); |
| 358 onoff1.SetAttribute("OffTime", |
| 359 StringValue("ns3::ConstantRandomVariable[Constant=0.0]")); |
| 360 onoff1.SetAttribute("PacketSize", UintegerValue(packetSize)); |
| 361 onoff1.SetAttribute("DataRate", DataRateValue(DataRate(rate))); |
| 362 |
| 363 ApplicationContainer apps1 = onoff1.Install( |
| 364 adhocNodes.Get(i + nWifis - nSinks)); |
| 365 apps1.Start(Seconds(dataStart + i * randomStartTime)); |
| 366 apps1.Stop(Seconds(dataTime + i * randomStartTime)); |
| 367 } |
| 368 |
| 369 NS_LOG_INFO("Run Simulation."); |
| 370 // prepare collection of statistics data for number of received and dropped pa
ckets |
| 371 for (uint32_t i = 0; i < nWifis; i++) |
| 372 { |
| 373 DynamicCast<WifiNetDevice>(allDevices.Get(i))->GetPhy()->TraceConnectWitho
utContext( |
| 374 "PhyRxDrop", MakeCallback(&PhyRxDrop)); |
| 375 DynamicCast<WifiNetDevice>(allDevices.Get(i))->GetPhy()->TraceConnectWitho
utContext( |
| 376 "PhyRxEnd", MakeCallback(&PhyRxRecv)); |
| 377 adhocNodes.Get(i)->GetObject<dsr::DsrRouting>()->TraceConnectWithoutContex
t( |
| 378 "Rx", MakeCallback(&RoutingRxRecv)); |
| 379 adhocNodes.Get(i)->GetObject<dsr::DsrRouting>()->TraceConnectWithoutContex
t( |
| 380 "Drop", MakeCallback(&RoutingRxDrop)); |
| 381 } |
| 382 Simulator::Stop(Seconds(TotalTime)); |
| 383 Simulator::Run(); |
| 384 SaveNodesEnergyStats(nWifis, nSinks, nodeSpeed, pauseTime, TotalTime, rate, |
| 385 dParam, sources, adhocNodes, lear); |
| 386 Simulator::Destroy(); |
| 387 } |
| 388 |
OLD | NEW |