Index: src/dsr/examples/dsr-lear.cc |
=================================================================== |
new file mode 100644 |
--- /dev/null |
+++ b/src/dsr/examples/dsr-lear.cc |
@@ -0,0 +1,388 @@ |
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
+/* |
+ * Copyright (c) 2011 Yufei Cheng |
+ * |
+ * This program is free software; you can redistribute it and/or modify |
+ * it under the terms of the GNU General Public License version 2 as |
+ * published by the Free Software Foundation; |
+ * |
+ * This program is distributed in the hope that it will be useful, |
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of |
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
+ * GNU General Public License for more details. |
+ * |
+ * You should have received a copy of the GNU General Public License |
+ * along with this program; if not, write to the Free Software |
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
+ * |
+ * Author: Yufei Cheng <yfcheng@ittc.ku.edu> |
+ * |
+ * James P.G. Sterbenz <jpgs@ittc.ku.edu>, director |
+ * ResiliNets Research Group http://wiki.ittc.ku.edu/resilinets |
+ * Information and Telecommunication Technology Center (ITTC) |
+ * and Department of Electrical Engineering and Computer Science |
+ * The University of Kansas Lawrence, KS USA. |
+ * |
+ * Work supported in part by NSF FIND (Future Internet Design) Program |
+ * under grant CNS-0626918 (Postmodern Internet Architecture), |
+ * NSF grant CNS-1050226 (Multilayer Network Resilience Analysis and Experimentation on GENI), |
+ * US Department of Defense (DoD), and ITTC at The University of Kansas. |
+ * |
+ * Author of LEAR modification: Tomasz Seweryn <tomasz.seweryn7@gmail.com> |
+ */ |
+ |
+#include "ns3/core-module.h" |
+#include "ns3/network-module.h" |
+#include "ns3/applications-module.h" |
+#include "ns3/mobility-module.h" |
+#include "ns3/config-store-module.h" |
+#include "ns3/wifi-module.h" |
+#include "ns3/internet-module.h" |
+#include "ns3/dsr-module.h" |
+#include <sstream> |
+#include "ns3/energy-module.h" |
+ |
+/* |
+ * Example simulation code for comparison of DSR and DSR-LEAR. |
+ * It generates *.pcap files with network flow for all network nodes. |
+ * These files can be used for further calculations of number of packets |
+ * (sent to)/(received by) the destination nodes and, as a result, become |
+ * basis for further calculations like PLR. Useful bash commands helping |
+ * in retrieving the number of unique packets delivered to destination nodes: |
+ * |
+ * tshark -R "data.len>1024 && ip.src==IP_SRC_ADDR && ip.dst==IP_DST_ADDR |
+ * && wlan.fc.retry != 1" -Tfields -e ip.id -r file_for_one_of_nodes.pcap | sort | uniq | wc -l |
+ * |
+ * In the end of the simulation remaining energy and consumed energy are saved |
+ * to *.txt files. For metrics like standard deviation one has to parse |
+ * these files and perform calculations in bash script or another program. |
+ * |
+ * This simulation can be used to generate data, needed for calculation of |
+ * metrics for comparing DSR and DSR-LEAR the same way authors of this |
+ * energy model described it in their publication: |
+ * http://dl.acm.org/citation.cfm?id=882570 |
+ */ |
+ |
+using namespace ns3; |
+NS_LOG_COMPONENT_DEFINE("DsrTest"); |
+ |
+//tested parameters |
+uint32_t g_droppedPacketsCount = 0; |
+uint32_t g_receivedPacketsCount = 0; |
+uint32_t g_routingDroppedPacketsCount = 0; |
+uint32_t g_routingReceivedPacketsCount = 0; |
+ |
+void |
+PhyRxDrop(Ptr<const Packet> p) |
+{ |
+ g_droppedPacketsCount++; |
+} |
+ |
+void |
+PhyRxRecv(Ptr<const Packet> p) |
+{ |
+ g_receivedPacketsCount++; |
+} |
+ |
+void |
+RoutingRxRecv(Ptr<const Packet> p) |
+{ |
+ g_routingReceivedPacketsCount++; |
+} |
+ |
+void |
+RoutingRxDrop(Ptr<const Packet> p) |
+{ |
+ g_routingDroppedPacketsCount++; |
+} |
+ |
+void |
+SaveNodesEnergyStats(uint32_t nWifis, uint32_t nSinks, double nodeSpeed, |
+ double pauseTime, double TotalTime, std::string rate, double dParam, |
+ EnergySourceContainer sources, NodeContainer adhocNodes, bool lear) |
+{ |
+ AsciiTraceHelper ascii; |
+ std::ostringstream o; |
+ if (lear) |
+ { |
+ o << "dsrLearDropRecvStats" << "_nW" << nWifis << "_nSi" << nSinks |
+ << "_nS" << nodeSpeed << "_pT" << pauseTime << "_tT" << TotalTime |
+ << "_" << rate << "_dP" << dParam << "_.txt"; |
+ } |
+ else |
+ { |
+ o << "dsrDropRecvStats" << "_nW" << nWifis << "_nSi" << nSinks << "_nS" |
+ << nodeSpeed << "_pT" << pauseTime << "_tT" << TotalTime << "_" |
+ << rate << "_dP" << dParam << "_.txt"; |
+ } |
+ |
+ Ptr<OutputStreamWrapper> stream = ascii.CreateFileStream(o.str()); |
+ // save RXDrop, RXRecv counters to the output file |
+ *stream->GetStream() << "PhyRxDropCntr,PhyRxRecvCntr," |
+ << g_droppedPacketsCount << "," << g_receivedPacketsCount << std::endl; |
+ *stream->GetStream() << "RoutingRxDropCntr,RoutingRxRecvCntr," |
+ << g_routingDroppedPacketsCount << "," << g_routingReceivedPacketsCount |
+ << std::endl; |
+ // save remaining energy stats and consumed energy stats |
+ std::ostringstream o1, o2; |
+ if (lear) |
+ { |
+ o1 << "dsr-lear-remainingEnergy" << "_nW" << nWifis << "_nSi" << nSinks |
+ << "_nS" << nodeSpeed << "_pT" << pauseTime << "_tT" << TotalTime |
+ << "_" << rate << "_dP" << dParam; |
+ o2 << "dsr-lear-consumedEnergy" << "_nW" << nWifis << "_nSi" << nSinks |
+ << "_nS" << nodeSpeed << "_pT" << pauseTime << "_tT" << TotalTime |
+ << "_" << rate << "_dP" << dParam; |
+ } |
+ else |
+ { |
+ o1 << "dsr-remainingEnergy" << "_nW" << nWifis << "_nSi" << nSinks |
+ << "_nS" << nodeSpeed << "_pT" << pauseTime << "_tT" << TotalTime |
+ << "_" << rate << "_dP" << dParam; |
+ o2 << "dsr-consumedEnergy" << "_nW" << nWifis << "_nSi" << nSinks << "_nS" |
+ << nodeSpeed << "_pT" << pauseTime << "_tT" << TotalTime << "_" |
+ << rate << "_dP" << dParam; |
+ } |
+ Ptr<OutputStreamWrapper> remainingEnergyFile = ascii.CreateFileStream( |
+ o1.str()); |
+ Ptr<OutputStreamWrapper> consumedEnergyFile = ascii.CreateFileStream( |
+ o2.str()); |
+ for (uint32_t i = 0; i < nWifis; i++) |
+ { |
+ std::ostringstream oss1, oss2, oss3; |
+ Ptr<BasicEnergySource> basicEnergySource; |
+ basicEnergySource = DynamicCast<BasicEnergySource>(sources.Get(i)); |
+ oss1 << adhocNodes.Get(i)->GetId() << " " |
+ << basicEnergySource->GetRemainingEnergy() << std::endl; |
+ *remainingEnergyFile->GetStream() << oss1.str(); |
+ // device energy model |
+ Ptr<DeviceEnergyModel> basicRadioModelPtr = |
+ basicEnergySource->FindDeviceEnergyModels("ns3::WifiRadioEnergyModel").Get( |
+ 0); |
+ NS_ASSERT(basicRadioModelPtr != NULL); |
+ oss2 << adhocNodes.Get(i)->GetId() << " " |
+ << basicRadioModelPtr->GetTotalEnergyConsumption() << std::endl; |
+ *consumedEnergyFile->GetStream() << oss2.str(); |
+ } |
+} |
+ |
+int |
+main(int argc, char *argv[]) |
+{ |
+ // |
+ // Users may find it convenient to turn on explicit debugging |
+ // for selected modules; the below lines suggest how to do this |
+ // |
+#if 0 |
+ LogComponentEnable ("Ipv4L3Protocol", LOG_LEVEL_ALL); |
+ LogComponentEnable ("UdpL4Protocol", LOG_LEVEL_ALL); |
+ LogComponentEnable ("UdpSocketImpl", LOG_LEVEL_ALL); |
+ LogComponentEnable ("NetDevice", LOG_LEVEL_ALL); |
+ LogComponentEnable ("PacketSink", LOG_LEVEL_ALL); |
+ LogComponentEnable ("OnOffApplication", LOG_LEVEL_ALL); |
+ LogComponentEnable ("Ipv4EndPointDemux", LOG_LEVEL_ALL); |
+#endif |
+ |
+#if 0 |
+ LogComponentEnable ("DsrOptions", LOG_LEVEL_ALL); |
+ LogComponentEnable ("DsrHelper", LOG_LEVEL_ALL); |
+ LogComponentEnable ("DsrRouting", LOG_LEVEL_ALL); |
+ LogComponentEnable ("DsrOptionHeader", LOG_LEVEL_ALL); |
+ LogComponentEnable ("DsrFsHeader", LOG_LEVEL_ALL); |
+ LogComponentEnable ("DsrGraReplyTable", LOG_LEVEL_ALL); |
+ LogComponentEnable ("DsrSendBuffer", LOG_LEVEL_ALL); |
+ LogComponentEnable ("RouteCache", LOG_LEVEL_ALL); |
+ LogComponentEnable ("DsrMaintainBuffer", LOG_LEVEL_ALL); |
+ LogComponentEnable ("RreqTable", LOG_LEVEL_ALL); |
+ LogComponentEnable ("DsrErrorBuffer", LOG_LEVEL_ALL); |
+ LogComponentEnable ("DsrNetworkQueue", LOG_LEVEL_ALL); |
+#endif |
+ |
+ NS_LOG_INFO("creating nodes"); |
+ |
+ // General parameters |
+ uint32_t nWifis = 40; |
+ uint32_t nSinks = 5; |
+ double TotalTime = 500.0; |
+ double dataTime = 500.0; |
+ double ppers = 1; |
+ uint32_t packetSize = 1024; |
+ double dataStart = 1.0; // start sending data at 1s |
+ bool lear = true; |
+ double initialEnergy = 10.0; |
+ double threshold = 9.9; |
+ double dParam = 0.1; |
+ |
+ //mobility parameters |
+ double pauseTime = 0.0; |
+ double nodeSpeed = 20.0; |
+ double txpDistance = 250.0; |
+ |
+ std::string rate = "40960bps"; |
+ std::string dataMode("DsssRate11Mbps"); |
+ std::string phyMode("DsssRate11Mbps"); |
+ |
+ //Allow users to override the default parameters and set it to new ones from CommandLine. |
+ CommandLine cmd; |
+ cmd.AddValue("nWifis", "Number of wifi nodes", nWifis); |
+ cmd.AddValue("nSinks", "Number of SINK traffic nodes", nSinks); |
+ cmd.AddValue("rate", "CBR traffic rate(in bps)", rate); |
+ cmd.AddValue("nodeSpeed", "Node speed in RandomWayPoint model", nodeSpeed); |
+ cmd.AddValue("packetSize", "The packet size", packetSize); |
+ cmd.AddValue("txpDistance", "Specify node's transmit range", txpDistance); |
+ cmd.AddValue("pauseTime", "pauseTime for mobility model", pauseTime); |
+ cmd.AddValue("lear", "Enable LEAR version of DSR", lear); |
+ cmd.AddValue("initialEnergy", "Initial energy for nodes", initialEnergy); |
+ cmd.AddValue("threshold", "Threshold parameter of LEAR algorithm", threshold); |
+ cmd.AddValue("dParam", "\"d\" parameter of LEAR algorithm", dParam); |
+ cmd.Parse(argc, argv); |
+ |
+ SeedManager::SetSeed(877); |
+ SeedManager::SetRun(1); |
+ |
+ NodeContainer adhocNodes; |
+ adhocNodes.Create(nWifis); |
+ NetDeviceContainer allDevices; |
+ |
+ NS_LOG_INFO("setting the default phy and channel parameters"); |
+ Config::SetDefault("ns3::WifiRemoteStationManager::NonUnicastMode", |
+ StringValue(phyMode)); |
+ Config::SetDefault("ns3::WifiRemoteStationManager::RtsCtsThreshold", |
+ StringValue("2200")); |
+ // disable fragmentation for frames below 2200 bytes |
+ Config::SetDefault("ns3::WifiRemoteStationManager::FragmentationThreshold", |
+ StringValue("2200")); |
+ |
+ WifiHelper wifi; |
+ wifi.SetStandard(WIFI_PHY_STANDARD_80211b); |
+ YansWifiPhyHelper wifiPhy = YansWifiPhyHelper::Default(); |
+ |
+ YansWifiChannelHelper wifiChannel; |
+ wifiChannel.SetPropagationDelay("ns3::ConstantSpeedPropagationDelayModel"); |
+ wifiChannel.AddPropagationLoss("ns3::RangePropagationLossModel", "MaxRange", |
+ DoubleValue(txpDistance)); |
+ wifiPhy.SetChannel(wifiChannel.Create()); |
+ |
+ // Add a non-QoS upper mac, and disable rate control |
+ NqosWifiMacHelper wifiMac = NqosWifiMacHelper::Default(); |
+ wifi.SetRemoteStationManager("ns3::ConstantRateWifiManager", "DataMode", |
+ StringValue(dataMode), "ControlMode", StringValue(phyMode)); |
+ |
+ wifiMac.SetType("ns3::AdhocWifiMac"); |
+ allDevices = wifi.Install(wifiPhy, wifiMac, adhocNodes); |
+ |
+ NS_LOG_INFO("Configure Tracing."); |
+ |
+ if (!lear) |
+ { |
+ wifiPhy.EnablePcapAll("dsr", false); |
+ } |
+ else |
+ { |
+ wifiPhy.EnablePcapAll("dsrlear", false); |
+ } |
+ |
+ MobilityHelper adhocMobility; |
+ ObjectFactory pos; |
+ pos.SetTypeId("ns3::RandomRectanglePositionAllocator"); |
+ pos.Set("X", StringValue("ns3::UniformRandomVariable[Min=0.0|Max=1000.0]")); |
+ pos.Set("Y", StringValue("ns3::UniformRandomVariable[Min=0.0|Max=1000.0]")); |
+ Ptr<PositionAllocator> taPositionAlloc = pos.Create()->GetObject< |
+ PositionAllocator>(); |
+ |
+ std::ostringstream speedUniformRandomVariableStream; |
+ speedUniformRandomVariableStream << "ns3::UniformRandomVariable[Min=0.0|Max=" |
+ << nodeSpeed << "]"; |
+ |
+ std::ostringstream pauseConstantRandomVariableStream; |
+ pauseConstantRandomVariableStream << "ns3::ConstantRandomVariable[Constant=" |
+ << pauseTime << "]"; |
+ |
+ adhocMobility.SetMobilityModel("ns3::RandomWaypointMobilityModel", |
+ // "Speed", StringValue ("ns3::UniformRandomVariable[Min=0.0|Max=nodeSpeed]"), |
+ "Speed", StringValue(speedUniformRandomVariableStream.str()), "Pause", |
+ StringValue(pauseConstantRandomVariableStream.str()), "PositionAllocator", |
+ PointerValue(taPositionAlloc)); |
+ adhocMobility.Install(adhocNodes); |
+ |
+ /** Energy Model **/ |
+ /***************************************************************************/ |
+ /* energy source */ |
+ BasicEnergySourceHelper basicSourceHelper; |
+ // configure energy source |
+ basicSourceHelper.Set("BasicEnergySourceInitialEnergyJ", |
+ DoubleValue(initialEnergy)); |
+ // install source |
+ EnergySourceContainer sources = basicSourceHelper.Install(adhocNodes); |
+ /* device energy model */ |
+ WifiRadioEnergyModelHelper radioEnergyHelper; |
+ // install device model |
+ DeviceEnergyModelContainer deviceModels = radioEnergyHelper.Install( |
+ allDevices, sources); |
+ /***************************************************************************/ |
+ |
+ InternetStackHelper internet; |
+ DsrMainHelper dsrMain; |
+ DsrHelper dsr; |
+ internet.Install(adhocNodes); |
+ if (lear) |
+ { |
+ dsr.Set("Lear", BooleanValue(true)); |
+ dsr.Set("ThresholdJ", DoubleValue(threshold)); |
+ dsr.Set("DParamJ", DoubleValue(dParam)); |
+ dsr.Set("DropRequestIdSize", UintegerValue(160)); |
+ } |
+ dsrMain.Install(dsr, adhocNodes); |
+ |
+ NS_LOG_INFO("assigning ip address"); |
+ Ipv4AddressHelper address; |
+ address.SetBase("10.1.1.0", "255.255.255.0"); |
+ Ipv4InterfaceContainer allInterfaces; |
+ allInterfaces = address.Assign(allDevices); |
+ |
+ uint16_t port = 9; |
+ double randomStartTime = (1 / ppers) / nSinks; //distributed btw 1s evenly as we are sending 1pkt/s |
+ |
+ for (uint32_t i = 0; i < nSinks; ++i) |
+ { |
+ PacketSinkHelper sink("ns3::UdpSocketFactory", |
+ InetSocketAddress(Ipv4Address::GetAny(), port)); |
+ ApplicationContainer apps_sink = sink.Install(adhocNodes.Get(i)); |
+ apps_sink.Start(Seconds(0.0)); |
+ apps_sink.Stop(Seconds(TotalTime)); |
+ |
+ OnOffHelper onoff1("ns3::UdpSocketFactory", |
+ Address(InetSocketAddress(allInterfaces.GetAddress(i), port))); |
+ onoff1.SetAttribute("OnTime", |
+ StringValue("ns3::ConstantRandomVariable[Constant=1.0]")); |
+ onoff1.SetAttribute("OffTime", |
+ StringValue("ns3::ConstantRandomVariable[Constant=0.0]")); |
+ onoff1.SetAttribute("PacketSize", UintegerValue(packetSize)); |
+ onoff1.SetAttribute("DataRate", DataRateValue(DataRate(rate))); |
+ |
+ ApplicationContainer apps1 = onoff1.Install( |
+ adhocNodes.Get(i + nWifis - nSinks)); |
+ apps1.Start(Seconds(dataStart + i * randomStartTime)); |
+ apps1.Stop(Seconds(dataTime + i * randomStartTime)); |
+ } |
+ |
+ NS_LOG_INFO("Run Simulation."); |
+ // prepare collection of statistics data for number of received and dropped packets |
+ for (uint32_t i = 0; i < nWifis; i++) |
+ { |
+ DynamicCast<WifiNetDevice>(allDevices.Get(i))->GetPhy()->TraceConnectWithoutContext( |
+ "PhyRxDrop", MakeCallback(&PhyRxDrop)); |
+ DynamicCast<WifiNetDevice>(allDevices.Get(i))->GetPhy()->TraceConnectWithoutContext( |
+ "PhyRxEnd", MakeCallback(&PhyRxRecv)); |
+ adhocNodes.Get(i)->GetObject<dsr::DsrRouting>()->TraceConnectWithoutContext( |
+ "Rx", MakeCallback(&RoutingRxRecv)); |
+ adhocNodes.Get(i)->GetObject<dsr::DsrRouting>()->TraceConnectWithoutContext( |
+ "Drop", MakeCallback(&RoutingRxDrop)); |
+ } |
+ Simulator::Stop(Seconds(TotalTime)); |
+ Simulator::Run(); |
+ SaveNodesEnergyStats(nWifis, nSinks, nodeSpeed, pauseTime, TotalTime, rate, |
+ dParam, sources, adhocNodes, lear); |
+ Simulator::Destroy(); |
+} |
+ |