From a93cef48ad3f25be1f12f5b675834a116401fadb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Deronne?= Date: Mon, 2 Jan 2017 08:46:09 +0100 Subject: [PATCH] examples: Add example wifi-multiple-tos --- examples/wireless/examples-to-run.py | 1 + examples/wireless/wifi-multi-tos.cc | 172 +++++++++++++++++++++++++++ examples/wireless/wscript | 3 + 3 files changed, 176 insertions(+) create mode 100644 examples/wireless/wifi-multi-tos.cc diff --git a/examples/wireless/examples-to-run.py b/examples/wireless/examples-to-run.py index cf972a021..91bd1d5c5 100755 --- a/examples/wireless/examples-to-run.py +++ b/examples/wireless/examples-to-run.py @@ -35,6 +35,7 @@ cpp_examples = [ ("mixed-bg-network --simulationTime=1", "True", "True"), ("wifi-aggregation --simulationTime=1", "True", "True"), ("80211e-txop --simulationTime=1", "True", "True"), + ("wifi-multi-tos --simulationTime=1 --nWifi=16 --useRts=1 --useShortGuardInterval=1", "True", "True"), ("wifi-tcp", "True", "True"), ("wifi-spectrum-per-example --distance=52 --index=3 --wifiType=ns3::SpectrumWifiPhy --simulationTime=1", "True", "True"), ("wifi-spectrum-per-example --distance=24 --index=31 --wifiType=ns3::YansWifiPhy --simulationTime=1", "True", "True"), diff --git a/examples/wireless/wifi-multi-tos.cc b/examples/wireless/wifi-multi-tos.cc new file mode 100644 index 000000000..ff5ff5993 --- /dev/null +++ b/examples/wireless/wifi-multi-tos.cc @@ -0,0 +1,172 @@ +/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 2016 + * + * 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: Sebastien Deronne + */ + +#include "ns3/core-module.h" +#include "ns3/applications-module.h" +#include "ns3/wifi-module.h" +#include "ns3/mobility-module.h" +#include "ns3/internet-module.h" + +// This is a simple example in order to show how to configure an IEEE 802.11n Wi-Fi network +// with multiple TOS. It outputs the aggregated UDP throughput, which depends on the number of +// stations, the HT MCS value (0 to 7), the channel width (20 or 40 MHz) and the guard interval +// (long or short). The user can also specify the distance between the access point and the +// stations (in meters), and can specify whether RTS/CTS is used or not. + +using namespace ns3; + +NS_LOG_COMPONENT_DEFINE ("WifiMultiTos"); + +int main (int argc, char *argv[]) +{ + uint32_t nWifi = 4; + double simulationTime = 10; //seconds + double distance = 1.0; //meters + uint16_t mcs = 7; + uint8_t channelWidth = 20; //MHz + bool useShortGuardInterval = false; + bool useRts = false; + + CommandLine cmd; + cmd.AddValue ("nWifi", "Number of stations", nWifi); + cmd.AddValue ("distance", "Distance in meters between the stations and the access point", distance); + cmd.AddValue ("simulationTime", "Simulation time in seconds", simulationTime); + cmd.AddValue ("useRts", "Enable/disable RTS/CTS", useRts); + cmd.AddValue ("mcs", "MCS value (0 - 7)", mcs); + cmd.AddValue ("channelWidth", "Channel width in MHz", channelWidth); + cmd.AddValue ("useShortGuardInterval", "Enable/disable short guard interval", useShortGuardInterval); + cmd.Parse (argc,argv); + + NodeContainer wifiStaNodes; + wifiStaNodes.Create (nWifi); + NodeContainer wifiApNode; + wifiApNode.Create (1); + + YansWifiChannelHelper channel = YansWifiChannelHelper::Default (); + YansWifiPhyHelper phy = YansWifiPhyHelper::Default (); + phy.SetChannel (channel.Create ()); + + // Set guard interval + phy.Set ("ShortGuardEnabled", BooleanValue (useShortGuardInterval)); + + WifiMacHelper mac; + WifiHelper wifi; + wifi.SetStandard (WIFI_PHY_STANDARD_80211n_5GHZ); + + std::ostringstream oss; + oss << "HtMcs" << mcs; + wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager", + "DataMode", StringValue (oss.str ()), + "ControlMode", StringValue (oss.str ()), + "RtsCtsThreshold", UintegerValue (useRts ? 0 : 999999)); + + Ssid ssid = Ssid ("ns3-80211n"); + + mac.SetType ("ns3::StaWifiMac", + "Ssid", SsidValue (ssid)); + + NetDeviceContainer staDevices; + staDevices = wifi.Install (phy, mac, wifiStaNodes); + + mac.SetType ("ns3::ApWifiMac", + "Ssid", SsidValue (ssid)); + + NetDeviceContainer apDevice; + apDevice = wifi.Install (phy, mac, wifiApNode); + + // Set channel width + Config::Set ("/NodeList/*/DeviceList/*/$ns3::WifiNetDevice/Phy/ChannelWidth", UintegerValue (channelWidth)); + + // mobility + MobilityHelper mobility; + Ptr positionAlloc = CreateObject (); + positionAlloc->Add (Vector (0.0, 0.0, 0.0)); + for (uint32_t i = 0; i< nWifi; i++) + { + positionAlloc->Add (Vector (distance, 0.0, 0.0)); + } + mobility.SetPositionAllocator (positionAlloc); + mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel"); + mobility.Install (wifiApNode); + mobility.Install (wifiStaNodes); + + // Internet stack + InternetStackHelper stack; + stack.Install (wifiApNode); + stack.Install (wifiStaNodes); + Ipv4AddressHelper address; + + address.SetBase ("192.168.1.0", "255.255.255.0"); + Ipv4InterfaceContainer staNodeInterfaces, apNodeInterface; + + staNodeInterfaces = address.Assign (staDevices); + apNodeInterface = address.Assign (apDevice); + + // Setting applications + ApplicationContainer sourceApplications, sinkApplications; + std::vector tosValues = {0x70, 0x28, 0xb8, 0xc0}; //AC_BE, AC_BK, AC_VI, AC_VO + uint32_t portNumber = 9; + for (uint8_t index = 0; index < nWifi; ++index) + { + for (uint8_t tosValue : tosValues) + { + auto ipv4 = wifiApNode.Get (0)->GetObject (); + const auto address = ipv4->GetAddress (1, 0).GetLocal (); + InetSocketAddress sinkSocket(address, portNumber++); + sinkSocket.SetTos(tosValue); + OnOffHelper onOffHelper ("ns3::UdpSocketFactory", sinkSocket); + onOffHelper.SetAttribute ("OnTime", StringValue ("ns3::ConstantRandomVariable[Constant=1]")); + onOffHelper.SetAttribute ("OffTime", StringValue ("ns3::ConstantRandomVariable[Constant=0]")); + onOffHelper.SetAttribute ("DataRate", DataRateValue (50000000/nWifi)); + onOffHelper.SetAttribute ("PacketSize", UintegerValue (1472)); //bytes + sourceApplications.Add (onOffHelper.Install (wifiStaNodes.Get (index))); + PacketSinkHelper packetSinkHelper ("ns3::UdpSocketFactory", sinkSocket); + sinkApplications.Add (packetSinkHelper.Install (wifiApNode.Get (0))); + } + } + + sinkApplications.Start (Seconds (0.0)); + sinkApplications.Stop (Seconds (simulationTime + 1)); + sourceApplications.Start (Seconds (1.0)); + sourceApplications.Stop (Seconds (simulationTime + 1)); + + Ipv4GlobalRoutingHelper::PopulateRoutingTables (); + + Simulator::Stop (Seconds (simulationTime + 1)); + Simulator::Run (); + Simulator::Destroy (); + + double throughput = 0; + for (unsigned index = 0; index < sinkApplications.GetN (); ++index) + { + uint32_t totalPacketsThrough = DynamicCast (sinkApplications.Get (index))->GetTotalRx (); + throughput += ((totalPacketsThrough * 8) / (simulationTime * 1000000.0)); //Mbit/s + } + if (throughput > 0) + { + std::cout << "Aggregated throughput: " << throughput << " Mbit/s" << std::endl; + } + else + { + NS_LOG_ERROR ("Obtained throughput is 0!"); + exit (1); + } + return 0; +} diff --git a/examples/wireless/wscript b/examples/wireless/wscript index 8459450db..151300bee 100644 --- a/examples/wireless/wscript +++ b/examples/wireless/wscript @@ -99,3 +99,6 @@ def build(bld): obj = bld.create_ns3_program('wifi-spectrum-saturation-example', ['internet', 'mobility', 'wifi', 'applications', 'spectrum']) obj.source = 'wifi-spectrum-saturation-example.cc' + + obj = bld.create_ns3_program('wifi-multi-tos', ['core','internet', 'mobility', 'wifi', 'applications', 'propagation']) + obj.source = 'wifi-multi-tos.cc'