diff options
| author | Loic Guegan <manzerberdes@gmx.com> | 2019-05-22 11:24:17 +0200 |
|---|---|---|
| committer | Loic Guegan <manzerberdes@gmx.com> | 2019-05-22 11:24:17 +0200 |
| commit | 8bdcd37ac44fe96d2c59424a24752f87f0444e36 (patch) | |
| tree | e31a0fe38c01bc6814a0b35474875fe538ea87c2 /src/ns3/nix/simulator/modules | |
| parent | 5a77b67d6baae0414310d29cab6f240963866062 (diff) | |
Update paper
Diffstat (limited to 'src/ns3/nix/simulator/modules')
| -rw-r--r-- | src/ns3/nix/simulator/modules/callbacks.cc | 12 | ||||
| -rw-r--r-- | src/ns3/nix/simulator/modules/energy.cc | 68 | ||||
| -rw-r--r-- | src/ns3/nix/simulator/modules/modules.hpp | 103 | ||||
| -rw-r--r-- | src/ns3/nix/simulator/modules/platform.cc | 137 |
4 files changed, 320 insertions, 0 deletions
diff --git a/src/ns3/nix/simulator/modules/callbacks.cc b/src/ns3/nix/simulator/modules/callbacks.cc new file mode 100644 index 0000000..4ae0c97 --- /dev/null +++ b/src/ns3/nix/simulator/modules/callbacks.cc @@ -0,0 +1,12 @@ + +#include "modules.hpp" + +void PktReceived(std::string nodeName,Ptr< const Packet > packet, const Address &address){ + NS_LOG_UNCOND("Node " << nodeName << " receive a packet" << " at time " << Simulator::Now ().GetSeconds () << "s"); +} + +void EnergyUpdated(std::string nodeName,double oldValue, double newValue){ + double currentTime=Simulator::Now ().GetSeconds (); + double energyConsumes=newValue-oldValue; + NS_LOG_UNCOND("Node " << nodeName << " consumes " << energyConsumes << "J" << " at time " << currentTime << "s"); +} diff --git a/src/ns3/nix/simulator/modules/energy.cc b/src/ns3/nix/simulator/modules/energy.cc new file mode 100644 index 0000000..56c38e5 --- /dev/null +++ b/src/ns3/nix/simulator/modules/energy.cc @@ -0,0 +1,68 @@ + +#include "modules.hpp" + +DeviceEnergyModelContainer setupCellEnergy(Cell cell){ + NodeContainer nodes(cell.first.first,cell.first.second); + NetDeviceContainer nodesNetDev(cell.second.first,cell.second.second); + + // Install energy source + BasicEnergySourceHelper edgeBasicSourceHelper; + edgeBasicSourceHelper.Set ("BasicEnergySourceInitialEnergyJ", DoubleValue (BASICENERGYSOURCEINITIALENERGYJ)); + edgeBasicSourceHelper.Set ("BasicEnergySupplyVoltageV", DoubleValue (BASICENERGYSUPPLYVOLTAGEV)); + EnergySourceContainer apEdgeNodesSources = edgeBasicSourceHelper.Install (cell.first.first); + EnergySourceContainer wifiEdgeNodesSources = edgeBasicSourceHelper.Install (cell.first.second); + + // Install device energy model + WifiRadioEnergyModelHelper radioEnergyHelper; + radioEnergyHelper.Set ("TxCurrentA", DoubleValue (TXCURRENTA)); + radioEnergyHelper.Set ("RxCurrentA", DoubleValue (RXCURRENTA)); + radioEnergyHelper.Set ("IdleCurrentA", DoubleValue (IDLECURRENTA)); + DeviceEnergyModelContainer edgeApDeviceModels = radioEnergyHelper.Install (cell.second.first, apEdgeNodesSources); + DeviceEnergyModelContainer edgeDeviceModels = radioEnergyHelper.Install (cell.second.second, wifiEdgeNodesSources); + + + // Trace + // DeviceEnergyModelContainer::Iterator it=edgeDeviceModels.Begin(); + //int i=1; // Node 0 will be AP, other node will have negative id (cf following while) + // This is usefull in logs, in fact ECOFEN nodes will have positive ID and WIFI energy nodes negative id + // AP will have id 0 in ECOFEN and WIFI (in order to combine their energy value when parsing logs + // while(it!=edgeDeviceModels.End()){ + // (*it)->TraceConnect ("TotalEnergyConsumption", std::to_string(0-i),MakeCallback (&EnergyUpdated)); + // it++; + // i++; + // } + // // AP will have id 0 + // (*edgeApDeviceModels.Begin())->TraceConnect ("TotalEnergyConsumption", std::to_string(0),MakeCallback (&EnergyUpdated)); + + // Ptr<BasicEnergySource> basicSourcePtr0 = DynamicCast<BasicEnergySource> (wifiEdgeNodesSources.Get (0)); + // //basicSourcePtr0->TraceConnectWithoutContext ("RemainingEnergy", MakeCallback (&RemainingEnergy)); + // //device energy model + // Ptr<DeviceEnergyModel> basicRadioModelPtr0 = + // basicSourcePtr0->FindDeviceEnergyModels ("ns3::WifiRadioEnergyModel").Get (0); + // NS_ASSERT (basicRadioModelPtr0 != NULL); + // basicRadioModelPtr0->TraceConnectWithoutContext ("TotalEnergyConsumption", MakeCallback (&TotalEnergy)); + return(DeviceEnergyModelContainer(edgeApDeviceModels,edgeDeviceModels)); +} + +void setupCloudEnergy(CloudInfos cloudInfos){ + NodeContainer cloudNodes=cloudInfos.first; + + // Install basic energy + ns3::BasicNodeEnergyHelper basicNodeEnergy; + basicNodeEnergy.Set("OnConso", ns3::DoubleValue (ONCONSO)); + basicNodeEnergy.Set("OffConso", ns3::DoubleValue (OFFCONSO)); + basicNodeEnergy.Install (cloudNodes); + + ns3::CompleteNetdeviceEnergyHelper completeNetdeviceEnergy; + completeNetdeviceEnergy.Set ("OffConso", ns3::DoubleValue (OFFCONSO)); + completeNetdeviceEnergy.Set ("IdleConso", ns3::DoubleValue (IDLECONSO)); + completeNetdeviceEnergy.Set ("RecvByteEnergy", ns3::DoubleValue (RECVBYTEENERGY)); + completeNetdeviceEnergy.Set ("SentByteEnergy", ns3::DoubleValue (SENTBYTEENERGY)); + completeNetdeviceEnergy.Set ("RecvPktEnergy", ns3::DoubleValue (RECVPKTENERGY)); + completeNetdeviceEnergy.Set ("SentPktEnergy", ns3::DoubleValue (SENTPKTENERGY)); + completeNetdeviceEnergy.Install(cloudNodes); + + ns3::ConsumptionLogger conso; + conso.NodeConso(ns3::Seconds (ECOFEN_LOG_EVERY), ns3::Seconds(SIM_TIME), cloudNodes); +} + diff --git a/src/ns3/nix/simulator/modules/modules.hpp b/src/ns3/nix/simulator/modules/modules.hpp new file mode 100644 index 0000000..7c38ad7 --- /dev/null +++ b/src/ns3/nix/simulator/modules/modules.hpp @@ -0,0 +1,103 @@ + +#ifndef MODULES_HPP +#define MODULES_HPP + +#include "ns3/command-line.h" +#include "ns3/config.h" +#include "ns3/string.h" +#include "ns3/log.h" +#include "ns3/yans-wifi-helper.h" +#include "ns3/ssid.h" +#include "ns3/mobility-helper.h" +#include "ns3/on-off-helper.h" +#include "ns3/yans-wifi-channel.h" +#include "ns3/mobility-model.h" +#include "ns3/packet-sink.h" +#include "ns3/packet-sink-helper.h" +#include "ns3/udp-echo-helper.h" +#include "ns3/tcp-westwood.h" +#include "ns3/internet-stack-helper.h" +#include "ns3/ipv4-address-helper.h" +#include "ns3/ipv4-global-routing-helper.h" +#include "ns3/constant-position-mobility-model.h" +#include "ns3/energy-module.h" +#include "ns3/wifi-radio-energy-model-helper.h" +#include "ns3/point-to-point-helper.h" +#include "ns3/ecofen-module.h" +#include "ns3/node-list.h" +#include "ns3/flow-monitor-module.h" + +// C++ library +#include <iostream> // Why not ? +#include <utility> // To use std::pair +#include <iomanip> // To use std::setw + +#define SIM_TIME 1800 // 30mins simulations +#define RECT_SIZE 20 // Sensors random rectangle position size +#define MAX_PACKET_BY_SENSOR 900000 // Reasonable big number (in order that simulation end before sensors stop sending packets) + +// ECOFEN +#define ECOFEN_LOG_EVERY 0.5 + +// WIFI Energy Values +#define BASICENERGYSOURCEINITIALENERGYJ 10000000 +#define BASICENERGYSUPPLYVOLTAGEV 3.3 +#define TXCURRENTA 0.38 +#define RXCURRENTA 0.313 +#define IDLECURRENTA 0.273 + +// Cloud Energy Values +#define ONCONSO 0 +#define OFFCONSO 0 +#define IDLECONSO 1 +#define RECVBYTEENERGY 3.4 +#define SENTBYTEENERGY 3.4 +#define RECVPKTENERGY 192.2 +#define SENTPKTENERGY 192.2 + +using namespace ns3; + +// ---------- Data types ---------- +typedef std::pair<NodeContainer,NodeContainer> CellNodes; // Format (APNode, SensorsNodes) +typedef std::pair<NetDeviceContainer,NetDeviceContainer> CellNetDevices; // Format (APNetDev, SensorsNetDev) +typedef std::pair<CellNodes,CellNetDevices> Cell; +typedef std::pair<Ipv4Address,int> EndPoint; // Format (IP,Port) +typedef std::pair<NodeContainer,EndPoint> CloudInfos; // Format (CloudHops,CloudEndPoint), here data sent to CloudEndPoint + + +// ---------- platform.cc ---------- +/** + * Create a WIFI cell paltform composed of nbSensors sensors and ap as an access point + */ +Cell createCell(uint32_t nbSensors, Ptr<ns3::Node> ap,int positionSeed); + +/** + * Build P2P network composed of nbHop hops (to simulate edge->cloud communications) + * Note: Cloud Servers are not considered here and completely ignored ! + */ +CloudInfos createCloud(int nbHop, uint32_t bandwidth, uint32_t latency); +/** + * Setup simulation scenario on the platforms. Sensors in cell will send packets of sensorsPktSize size every + * sensorsSensInterval second to the cloud using cloudInfos. + */ +void setupScenario(Cell cell, CloudInfos cloudInfos, int sensorsPktSize, int sensorsSendInterval); + + +// ---------- energy.cc ---------- +/* + * Configure WIFI energy module for cell + */ +DeviceEnergyModelContainer setupCellEnergy(Cell cell); +/* + * Configure link/port energy using ecofen + */ +void setupCloudEnergy(CloudInfos cloudInfos); + + +// ---------- callbacks.cc ---------- +void PktReceived(std::string nodeName,Ptr< const Packet > packet, const Address &address); +void EnergyUpdated(std::string nodeName,double oldValue, double newValue); + + + +#endif diff --git a/src/ns3/nix/simulator/modules/platform.cc b/src/ns3/nix/simulator/modules/platform.cc new file mode 100644 index 0000000..7cfc2b9 --- /dev/null +++ b/src/ns3/nix/simulator/modules/platform.cc @@ -0,0 +1,137 @@ +#include "modules.hpp" +#include "ns3/pointer.h" + +/** + * Create a sensors cell base on + * nbSensors Number of temperature sensors in the cell + * ap the Access Point (usually linked to the cloud) + */ +Cell createCell(uint32_t nbSensors, Ptr<ns3::Node> ap,int positionSeed){ + // Create sensors + NodeContainer sensors; + sensors.Create(nbSensors); + + + // Define sensors position/mobility + MobilityHelper mobility; + mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel"); // Sensors are fixed + Ptr<UniformRandomVariable> X = CreateObject<UniformRandomVariable> (); + X->SetAttribute ("Min", DoubleValue (0)); + X->SetAttribute ("Max", DoubleValue (RECT_SIZE)); + X->SetAttribute("Stream",IntegerValue(positionSeed)); + Ptr<UniformRandomVariable> Y = CreateObject<UniformRandomVariable> (); + Y->SetAttribute ("Min", DoubleValue (0)); + Y->SetAttribute ("Max", DoubleValue (RECT_SIZE)); + Y->SetAttribute("Stream",IntegerValue(positionSeed+1)); + mobility.SetPositionAllocator("ns3::RandomRectanglePositionAllocator", + "X",PointerValue(X), + "Y",PointerValue(Y)); + mobility.Install(NodeContainer(ap,sensors)); + + // To apply XXWifiPhy and WifiMac on sensors + WifiHelper wifiHelper; + wifiHelper.SetStandard (WIFI_PHY_STANDARD_80211n_5GHZ); + + /* Set up Legacy Channel */ + YansWifiChannelHelper wifiChannel; + wifiChannel.SetPropagationDelay ("ns3::ConstantSpeedPropagationDelayModel"); + wifiChannel.AddPropagationLoss ("ns3::FriisPropagationLossModel", "Frequency", DoubleValue (5e9)); + + /* Setup Physical Layer */ + YansWifiPhyHelper wifiPhy = YansWifiPhyHelper::Default (); + wifiPhy.SetChannel (wifiChannel.Create ()); + wifiPhy.Set ("TxPowerStart", DoubleValue (10.0)); + wifiPhy.Set ("TxPowerEnd", DoubleValue (10.0)); + wifiPhy.Set ("TxPowerLevels", UintegerValue (1)); + wifiPhy.Set ("TxGain", DoubleValue (0)); + wifiPhy.Set ("RxGain", DoubleValue (0)); + wifiPhy.Set ("RxNoiseFigure", DoubleValue (10)); + wifiPhy.Set ("CcaMode1Threshold", DoubleValue (-79)); + wifiPhy.Set ("EnergyDetectionThreshold", DoubleValue (-79 + 3)); + // wifiPhy.SetErrorRateModel ("ns3::YansErrorRateModel"); + wifiHelper.SetRemoteStationManager ("ns3::ConstantRateWifiManager", + "DataMode", StringValue ("HtMcs7"), + "ControlMode", StringValue ("HtMcs0")); + /* Configure AP */ + Ssid ssid = Ssid ("network"); + WifiMacHelper wifiMac; + wifiMac.SetType ("ns3::ApWifiMac", "Ssid", SsidValue (ssid)); + NetDeviceContainer apNetDevice; + apNetDevice = wifiHelper.Install (wifiPhy, wifiMac, ap); + /* Configure STA */ + wifiMac.SetType ("ns3::StaWifiMac", "Ssid", SsidValue (ssid)); + NetDeviceContainer sensorsNetDevices; + sensorsNetDevices = wifiHelper.Install (wifiPhy, wifiMac, sensors); + + return(std::make_pair(std::make_pair(ap,sensors),std::make_pair(apNetDevice,sensorsNetDevices))); +} + +/** + * Install network stack and applications + */ +void setupScenario(Cell cell, CloudInfos cloudInfos, int sensorsPktSize, int sensorsSendInterval){ + NodeContainer ap=cell.first.first; + NodeContainer sensors=cell.first.second; + NetDeviceContainer apNetDev= cell.second.first; + NetDeviceContainer sensorsNetDev= cell.second.second; + + // 6. Install TCP/IP stack & assign IP addresses + InternetStackHelper internet; + // internet.Install (ap); + internet.Install (sensors); + Ipv4AddressHelper ipv4; + ipv4.SetBase ("10.0.0.0", "255.255.0.0"); + Ipv4InterfaceContainer apInt,sensorsInt; + apInt=ipv4.Assign(apNetDev); + sensorsInt=ipv4.Assign(sensorsNetDev); + + UdpEchoClientHelper echoClientHelper (InetSocketAddress (cloudInfos.second.first, cloudInfos.second.second)); + echoClientHelper.SetAttribute ("Interval", TimeValue (Seconds (sensorsSendInterval))); + echoClientHelper.SetAttribute ("PacketSize", UintegerValue (sensorsPktSize)); + echoClientHelper.SetAttribute ("MaxPackets", UintegerValue (MAX_PACKET_BY_SENSOR)); + ApplicationContainer pingApps; + + // again using different start times to workaround Bug 388 and Bug 912 + for(int i=0;i<sensors.GetN();i++){ + echoClientHelper.SetAttribute ("StartTime", TimeValue (MilliSeconds (1+i))); + echoClientHelper.Install (sensors.Get(i)); + } +} + + +CloudInfos createCloud(int nbHop, uint32_t bandwidth, uint32_t latency){ + + NodeContainer HopNodes; + HopNodes.Create(nbHop); + InternetStackHelper stack; + stack.Install(HopNodes); + + Ipv4Address cloudIP; // Will be fill in the following for loop + int cloudPort=80; + for(int i=0;i<nbHop-1;i++){ + NodeContainer curNodes(HopNodes.Get(i),HopNodes.Get(i+1)); + + PointToPointHelper pointToPoint; + pointToPoint.SetDeviceAttribute ("DataRate", StringValue ((std::to_string(bandwidth)+"Mbps").c_str())); + pointToPoint.SetChannelAttribute ("Delay", StringValue ((std::to_string(latency)+"ms").c_str())); + + NetDeviceContainer p2pDevices; + p2pDevices = pointToPoint.Install (curNodes); + + Ipv4AddressHelper address; + address.SetBase (("10."+std::to_string(i+1)+".0.0").c_str(), "255.255.0.0"); // Remember: 10.0.0.0 is used by WIFI + Ipv4InterfaceContainer p2pInterfaces; + p2pInterfaces = address.Assign (p2pDevices); + + if(i==nbHop-2){ // If we are on the last for loop (before last node) + cloudIP=p2pInterfaces.GetAddress (1); // Get Last node interface + PacketSinkHelper apSink("ns3::UdpSocketFactory",InetSocketAddress (Ipv4Address::GetAny (), cloudPort)); + ApplicationContainer sinkApp=apSink.Install(curNodes.Get(1)); // Instal sink on last node + sinkApp.Get(0)->TraceConnect("Rx","CloudSwitch",MakeCallback(&PktReceived)); + sinkApp.Start (Seconds (0)); + } + } + + return(std::make_pair(HopNodes,std::make_pair(cloudIP,cloudPort))); + +} |
