A Discrete-Event Network Simulator
API
mixed-bg-network.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2016 S├ębastien Deronne
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: S├ębastien Deronne <sebastien.deronne@gmail.com>
19  */
20 
21 #include "ns3/core-module.h"
22 #include "ns3/network-module.h"
23 #include "ns3/applications-module.h"
24 #include "ns3/wifi-module.h"
25 #include "ns3/mobility-module.h"
26 #include "ns3/ipv4-global-routing-helper.h"
27 #include "ns3/internet-module.h"
28 
29 // This example shows how the presence of an 802.11b station in an 802.11g network does affect the performance.
30 //
31 // The example compares different scenarios depending on whether an 802.11b station is associated to the access point
32 // and depending on the configuration of the following parameters:
33 // - protection mode that is configured on the AP;
34 // - whether short PLCP is supported by the 802.11b station;
35 // - whether short slot time is supported by both the 802.11g station and the AP.
36 //
37 // The output results show that the presence of an 802.11b station strongly affects 802.11g performance.
38 // Protection mechanisms ensure that the NAV value of 802.11b stations is set correctly in case of 802.11g transmissions.
39 // In practice, those protection mechanism add a lot of overhead, resulting in reduced performance. CTS-To-Self introduces
40 // less overhead than Rts-Cts, but is not heard by hidden stations (and is thus generally only recommended as a protection
41 // mechanism for access points). Since short slot time is disabled once an 802.11b station enters the network, benefits from
42 // short slot time are only observed in a pure-G configuration.
43 //
44 // The user can also select the payload size and can choose either an UDP or a TCP connection.
45 // Example: ./waf --run "mixed-bg-network --isUdp=1"
46 //
47 // Network topology:
48 //
49 // STA (802.11b) AP (802.11b/g) STA (802.11b/g)
50 // * * *
51 // | | |
52 // n1 n2 n3
53 
54 using namespace ns3;
55 
56 NS_LOG_COMPONENT_DEFINE ("ErpAndNonErp");
57 
59 {
60 public:
61  Experiment ();
62  double Run (bool enableProtection, bool enableShortSlotTime, bool enableShortPlcpPreamble, bool isMixed, bool isUdp, uint32_t payloadSize, uint32_t simulationTime);
63 };
64 
66 {
67 }
68 
69 double
70 Experiment::Run (bool enableProtection, bool enableShortSlotTime, bool enableShortPlcpPreamble, bool isMixed, bool isUdp, uint32_t payloadSize, uint32_t simulationTime)
71 {
72  double throughput = 0;
73  uint32_t totalPacketsThrough = 0;
74  uint32_t nWifiB = 0;
75  if (isMixed)
76  {
77  nWifiB = 1;
78  }
79 
80  NodeContainer wifiBStaNodes;
81  wifiBStaNodes.Create (nWifiB);
82  NodeContainer wifiGStaNodes;
83  wifiGStaNodes.Create (1);
85  wifiApNode.Create (1);
86 
88  channel.AddPropagationLoss ("ns3::RangePropagationLossModel");
89 
92  phy.SetChannel (channel.Create ());
93 
95  wifi.SetRemoteStationManager ("ns3::IdealWifiManager");
96 
97  // 802.11b STA
99 
101  Ssid ssid = Ssid ("ns-3-ssid");
102 
103  mac.SetType ("ns3::StaWifiMac",
104  "Ssid", SsidValue (ssid),
105  "ShortSlotTimeSupported", BooleanValue (enableShortSlotTime));
106 
107  // Configure the PLCP preamble type: long or short
108  phy.Set ("ShortPlcpPreambleSupported", BooleanValue (enableShortPlcpPreamble));
109 
110  NetDeviceContainer bStaDevice;
111  bStaDevice = wifi.Install (phy, mac, wifiBStaNodes);
112 
113  // 802.11b/g STA
115  NetDeviceContainer gStaDevice;
116  gStaDevice = wifi.Install (phy, mac, wifiGStaNodes);
117 
118  // 802.11b/g AP
119  mac.SetType ("ns3::ApWifiMac",
120  "Ssid", SsidValue (ssid),
121  "BeaconGeneration", BooleanValue (true),
122  "EnableNonErpProtection", BooleanValue (enableProtection),
123  "ShortSlotTimeSupported", BooleanValue (enableShortSlotTime));
124 
125  NetDeviceContainer apDevice;
126  apDevice = wifi.Install (phy, mac, wifiApNode);
127 
128  // Setting mobility model
130  Ptr<ListPositionAllocator> positionAlloc = CreateObject<ListPositionAllocator> ();
131 
132  positionAlloc->Add (Vector (0.0, 0.0, 0.0));
133  if (isMixed)
134  {
135  positionAlloc->Add (Vector (5.0, 0.0, 0.0));
136  }
137  positionAlloc->Add (Vector (0.0, 5.0, 0.0));
138 
139  mobility.SetPositionAllocator (positionAlloc);
140  mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
141  mobility.Install (wifiApNode);
142  mobility.Install (wifiBStaNodes);
143  mobility.Install (wifiGStaNodes);
144 
145  // Internet stack
147  stack.Install (wifiApNode);
148  stack.Install (wifiBStaNodes);
149  stack.Install (wifiGStaNodes);
150 
152 
153  address.SetBase ("192.168.1.0", "255.255.255.0");
154  Ipv4InterfaceContainer bStaInterface;
155  bStaInterface = address.Assign (bStaDevice);
156  Ipv4InterfaceContainer gStaInterface;
157  gStaInterface = address.Assign (gStaDevice);
158  Ipv4InterfaceContainer ApInterface;
159  ApInterface = address.Assign (apDevice);
160 
161  // Setting applications
162  if (isUdp)
163  {
164  UdpServerHelper myServer (9);
165  ApplicationContainer serverApp = myServer.Install (wifiApNode);
166  serverApp.Start (Seconds (0.0));
167  serverApp.Stop (Seconds (simulationTime + 1));
168 
169  UdpClientHelper myClient (ApInterface.GetAddress (0), 9);
170  myClient.SetAttribute ("MaxPackets", UintegerValue (4294967295u));
171  myClient.SetAttribute ("Interval", TimeValue (Time ("0.0002"))); //packets/s
172  myClient.SetAttribute ("PacketSize", UintegerValue (payloadSize));
173 
174  ApplicationContainer clientApp = myClient.Install (wifiGStaNodes);
175  clientApp.Start (Seconds (1.0));
176  clientApp.Stop (Seconds (simulationTime + 1));
177 
178  Simulator::Stop (Seconds (simulationTime + 1));
179  Simulator::Run ();
181 
182  totalPacketsThrough = DynamicCast<UdpServer> (serverApp.Get (0))->GetReceived ();
183  throughput = totalPacketsThrough * payloadSize * 8 / (simulationTime * 1000000.0);
184  }
185  else
186  {
187  uint16_t port = 50000;
188  Address apLocalAddress (InetSocketAddress (Ipv4Address::GetAny (), port));
189  PacketSinkHelper packetSinkHelper ("ns3::TcpSocketFactory", apLocalAddress);
190 
191  ApplicationContainer sinkApp = packetSinkHelper.Install (wifiApNode.Get (0));
192  sinkApp.Start (Seconds (0.0));
193  sinkApp.Stop (Seconds (simulationTime + 1));
194 
195  OnOffHelper onoff ("ns3::TcpSocketFactory",Ipv4Address::GetAny ());
196  onoff.SetAttribute ("OnTime", StringValue ("ns3::ConstantRandomVariable[Constant=1]"));
197  onoff.SetAttribute ("OffTime", StringValue ("ns3::ConstantRandomVariable[Constant=0]"));
198  onoff.SetAttribute ("PacketSize", UintegerValue (payloadSize));
199  onoff.SetAttribute ("DataRate", DataRateValue (54000000)); //bit/s
200 
201  AddressValue remoteAddress (InetSocketAddress (ApInterface.GetAddress (0), port));
202  onoff.SetAttribute ("Remote", remoteAddress);
203 
205  apps.Add (onoff.Install (wifiGStaNodes));
206  apps.Start (Seconds (1.0));
207  apps.Stop (Seconds (simulationTime + 1));
208 
209  Simulator::Stop (Seconds (simulationTime + 1));
210  Simulator::Run ();
212 
213  totalPacketsThrough = DynamicCast<PacketSink> (sinkApp.Get (0))->GetTotalRx ();
214  throughput += totalPacketsThrough * 8 / (simulationTime * 1000000.0);
215  }
216 
217  return throughput;
218 }
219 
220 int main (int argc, char *argv[])
221 {
222  uint32_t payloadSize = 1472; //bytes
223  uint32_t simulationTime = 10; //seconds
224  bool isUdp = true;
225 
227  cmd.AddValue ("payloadSize", "Payload size in bytes", payloadSize);
228  cmd.AddValue ("simulationTime", "Simulation time in seconds", simulationTime);
229  cmd.AddValue ("isUdp", "UDP if set to 1, TCP otherwise", isUdp);
230  cmd.Parse (argc, argv);
231 
233  double throughput = 0;
234  std::cout << "Protection mode" << "\t\t" << "Slot time supported" << "\t\t" << "PLCP preamble supported" << "\t\t" << "Scenario" << "\t" << "Throughput" << std::endl;
235 
236  throughput = experiment.Run (false, false, false, false, isUdp, payloadSize, simulationTime);
237  if (throughput < 23 || throughput > 24)
238  {
239  NS_LOG_ERROR ("Obtained throughput " << throughput << " is not in the expected boundaries!");
240  exit (1);
241  }
242  std::cout << "Disabled" << "\t\t" << "Long" << "\t\t\t\t" << "Long" << "\t\t\t\t" << "G-only" << "\t\t" << throughput <<" Mbit/s" << std::endl;
243 
244  throughput = experiment.Run (false, true, false, false, isUdp, payloadSize, simulationTime);
245  if (throughput < 29 || throughput > 30)
246  {
247  NS_LOG_ERROR ("Obtained throughput " << throughput << " is not in the expected boundaries!");
248  exit (1);
249  }
250  std::cout << "Disabled" << "\t\t" << "Short" << "\t\t\t\t" << "Long" << "\t\t\t\t" << "G-only" << "\t\t" << throughput <<" Mbit/s" << std::endl;
251 
252  throughput = experiment.Run (false, false, false, true, isUdp, payloadSize, simulationTime);
253  if (throughput < 23 || throughput > 24)
254  {
255  NS_LOG_ERROR ("Obtained throughput " << throughput << " is not in the expected boundaries!");
256  exit (1);
257  }
258  std::cout << "Disabled" << "\t\t" << "Long" << "\t\t\t\t" << "Long" << "\t\t\t\t" << "Mixed" << "\t\t" << throughput <<" Mbit/s" << std::endl;
259 
260  throughput = experiment.Run (false, false, true, true, isUdp, payloadSize, simulationTime);
261  if (throughput < 23 || throughput > 24)
262  {
263  NS_LOG_ERROR ("Obtained throughput " << throughput << " is not in the expected boundaries!");
264  exit (1);
265  }
266  std::cout << "Disabled" << "\t\t" << "Long" << "\t\t\t\t" << "Short" << "\t\t\t\t" << "Mixed" << "\t\t" << throughput <<" Mbit/s" << std::endl;
267 
268  Config::SetDefault ("ns3::WifiRemoteStationManager::ProtectionMode", StringValue ("Rts-Cts"));
269 
270  throughput = experiment.Run (true, false, false, true, isUdp, payloadSize, simulationTime);
271  if (throughput < 19 || throughput > 20)
272  {
273  NS_LOG_ERROR ("Obtained throughput " << throughput << " is not in the expected boundaries!");
274  exit (1);
275  }
276  std::cout << "RTS/CTS" << "\t\t\t" << "Long" << "\t\t\t\t" << "Long" << "\t\t\t\t" << "Mixed" << "\t\t" << throughput <<" Mbit/s" << std::endl;
277 
278  throughput = experiment.Run (true, false, true, true, isUdp, payloadSize, simulationTime);
279  if (throughput < 19 || throughput > 20)
280  {
281  NS_LOG_ERROR ("Obtained throughput " << throughput << " is not in the expected boundaries!");
282  exit (1);
283  }
284  std::cout << "RTS/CTS" << "\t\t\t" << "Long" << "\t\t\t\t" << "Short" << "\t\t\t\t" << "Mixed" << "\t\t" << throughput <<" Mbit/s" << std::endl;
285 
286  Config::SetDefault ("ns3::WifiRemoteStationManager::ProtectionMode", StringValue ("Cts-To-Self"));
287 
288  throughput = experiment.Run (true, false, false, true, isUdp, payloadSize, simulationTime);
289  if (throughput < 21 || throughput > 22)
290  {
291  NS_LOG_ERROR ("Obtained throughput " << throughput << " is not in the expected boundaries!");
292  exit (1);
293  }
294  std::cout << "CTS-TO-SELF" << "\t\t" << "Long" << "\t\t\t\t" << "Long" << "\t\t\t\t" << "Mixed" << "\t\t" << throughput <<" Mbit/s" << std::endl;
295 
296  throughput = experiment.Run (true, false, true, true, isUdp, payloadSize, simulationTime);
297  if (throughput < 21 || throughput > 22)
298  {
299  NS_LOG_ERROR ("Obtained throughput " << throughput << " is not in the expected boundaries!");
300  exit (1);
301  }
302  std::cout << "CTS-TO-SELF" << "\t\t" << "Long" << "\t\t\t\t" << "Short" << "\t\t\t\t" << "Mixed" << "\t\t" << throughput <<" Mbit/s" << std::endl;
303 
304  return 0;
305 }
ERP-OFDM PHY (Clause 19, Section 19.5)
void AddPropagationLoss(std::string name, std::string n0="", const AttributeValue &v0=EmptyAttributeValue(), std::string n1="", const AttributeValue &v1=EmptyAttributeValue(), std::string n2="", const AttributeValue &v2=EmptyAttributeValue(), std::string n3="", const AttributeValue &v3=EmptyAttributeValue(), std::string n4="", const AttributeValue &v4=EmptyAttributeValue(), std::string n5="", const AttributeValue &v5=EmptyAttributeValue(), std::string n6="", const AttributeValue &v6=EmptyAttributeValue(), std::string n7="", const AttributeValue &v7=EmptyAttributeValue())
Helper class for UAN CW MAC example.
tuple channel
Definition: third.py:85
void Set(std::string name, const AttributeValue &v)
Definition: wifi-helper.cc:112
holds a vector of ns3::Application pointers.
void experiment(bool enableCtsRts)
Run single 10 seconds experiment with enabled or disabled RTS/CTS mechanism.
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:102
an Inet address class
static Ipv4Address GetAny(void)
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:73
AttributeValue implementation for Boolean.
Definition: boolean.h:34
holds a vector of std::pair of Ptr and interface index.
Ptr< YansWifiChannel > Create(void) const
void SetRemoteStationManager(std::string type, std::string n0="", const AttributeValue &v0=EmptyAttributeValue(), std::string n1="", const AttributeValue &v1=EmptyAttributeValue(), std::string n2="", const AttributeValue &v2=EmptyAttributeValue(), std::string n3="", const AttributeValue &v3=EmptyAttributeValue(), std::string n4="", const AttributeValue &v4=EmptyAttributeValue(), std::string n5="", const AttributeValue &v5=EmptyAttributeValue(), std::string n6="", const AttributeValue &v6=EmptyAttributeValue(), std::string n7="", const AttributeValue &v7=EmptyAttributeValue())
Definition: wifi-helper.cc:683
Hold variables of type string.
Definition: string.h:41
Make it easy to create and manage PHY objects for the yans model.
static YansWifiChannelHelper Default(void)
Create a channel helper in a default working state.
void Add(ApplicationContainer other)
Append the contents of another ApplicationContainer to the end of this container. ...
static void Run(void)
Run the simulation.
Definition: simulator.cc:201
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:201
aggregate IP/TCP/UDP functionality to existing Nodes.
A helper to make it easier to instantiate an ns3::PacketSinkApplication on a set of nodes...
static YansWifiPhyHelper Default(void)
Create a phy helper in a default working state.
helps to create WifiNetDevice objects
Definition: wifi-helper.h:231
A helper to make it easier to instantiate an ns3::OnOffApplication on a set of nodes.
Definition: on-off-helper.h:42
tuple cmd
Definition: second.py:35
void SetPcapDataLinkType(enum SupportedPcapDataLinkTypes dlt)
Set the data link type of PCAP traces to be used.
Definition: wifi-helper.cc:530
virtual NetDeviceContainer Install(const WifiPhyHelper &phy, const WifiMacHelper &mac, NodeContainer c) const
Definition: wifi-helper.cc:712
uint16_t port
Definition: dsdv-manet.cc:44
a polymophic address class
Definition: address.h:90
void SetChannel(Ptr< YansWifiChannel > channel)
void Install(Ptr< Node > node) const
"Layout" a single node according to the current position allocator type.
tuple mobility
Definition: third.py:101
tuple phy
Definition: third.py:86
Create a client application which sends UDP packets carrying a 32bit sequence number and a 64 bit tim...
AttributeValue implementation for Time.
Definition: nstime.h:957
Hold an unsigned integer type.
Definition: uinteger.h:44
holds a vector of ns3::NetDevice pointers
virtual void SetStandard(enum WifiPhyStandard standard)
Definition: wifi-helper.cc:706
Create a server application which waits for input UDP packets and uses the information carried into t...
double Run(bool enableProtection, bool enableShortSlotTime, bool enableShortPlcpPreamble, bool isMixed, bool isUdp, uint32_t payloadSize, uint32_t simulationTime)
tuple mac
Definition: third.py:92
void Start(Time start)
Arrange for all of the Applications in this container to Start() at the Time given as a parameter...
Parse command-line arguments.
Definition: command-line.h:205
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:165
tuple wifiApNode
Definition: third.py:83
void SetAttribute(std::string name, const AttributeValue &value)
Record an attribute to be set in each Application after it is is created.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
keep track of a set of node pointers.
Ptr< Application > Get(uint32_t i) const
Get the Ptr stored in this container at a given index.
DSSS PHY (Clause 15) and HR/DSSS PHY (Clause 18)
void SetMobilityModel(std::string type, std::string n1="", const AttributeValue &v1=EmptyAttributeValue(), std::string n2="", const AttributeValue &v2=EmptyAttributeValue(), std::string n3="", const AttributeValue &v3=EmptyAttributeValue(), std::string n4="", const AttributeValue &v4=EmptyAttributeValue(), std::string n5="", const AttributeValue &v5=EmptyAttributeValue(), std::string n6="", const AttributeValue &v6=EmptyAttributeValue(), std::string n7="", const AttributeValue &v7=EmptyAttributeValue(), std::string n8="", const AttributeValue &v8=EmptyAttributeValue(), std::string n9="", const AttributeValue &v9=EmptyAttributeValue())
void Install(std::string nodeName) const
Aggregate implementations of the ns3::Ipv4, ns3::Ipv6, ns3::Udp, and ns3::Tcp classes onto the provid...
tuple ssid
Definition: third.py:93
manage and create wifi channel objects for the yans model.
create MAC layers for a ns3::WifiNetDevice.
tuple stack
Definition: first.py:34
The IEEE 802.11 SSID Information Element.
Definition: ssid.h:38
virtual void SetType(std::string type, std::string n0="", const AttributeValue &v0=EmptyAttributeValue(), std::string n1="", const AttributeValue &v1=EmptyAttributeValue(), std::string n2="", const AttributeValue &v2=EmptyAttributeValue(), std::string n3="", const AttributeValue &v3=EmptyAttributeValue(), std::string n4="", const AttributeValue &v4=EmptyAttributeValue(), std::string n5="", const AttributeValue &v5=EmptyAttributeValue(), std::string n6="", const AttributeValue &v6=EmptyAttributeValue(), std::string n7="", const AttributeValue &v7=EmptyAttributeValue(), std::string n8="", const AttributeValue &v8=EmptyAttributeValue(), std::string n9="", const AttributeValue &v9=EmptyAttributeValue(), std::string n10="", const AttributeValue &v10=EmptyAttributeValue())
Helper class used to assign positions and mobility models to nodes.
AttributeValue implementation for Address.
Definition: address.h:278
void Stop(Time stop)
Arrange for all of the Applications in this container to Stop() at the Time given as a parameter...
Ipv4InterfaceContainer Assign(const NetDeviceContainer &c)
Assign IP addresses to the net devices specified in the container based on the current network prefix...
ApplicationContainer Install(NodeContainer c)
Create one UDP server application on each of the Nodes in the NodeContainer.
AttributeValue implementation for DataRate.
Definition: data-rate.h:242
void AddValue(const std::string &name, const std::string &help, T &value)
Add a program argument, assigning to POD.
Definition: command-line.h:495
static void Stop(void)
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:209
Ptr< Node > Get(uint32_t i) const
Get the Ptr stored in this container at a given index.
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:895
AttributeValue implementation for Ssid.
Definition: ssid.h:95
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:774
void Add(Vector v)
Add a position to the list of positions.
ApplicationContainer Install(NodeContainer c) const
Install an ns3::PacketSinkApplication on each node of the input container configured with all the att...
void Parse(int argc, char *argv[])
Parse the program arguments.
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
#define NS_LOG_ERROR(msg)
Use NS_LOG to output a message of level LOG_ERROR.
Definition: log.h:220
tuple wifi
Definition: third.py:89
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
tuple address
Definition: first.py:37
void SetPositionAllocator(Ptr< PositionAllocator > allocator)
Set the position allocator which will be used to allocate the initial position of every node initiali...
ApplicationContainer Install(NodeContainer c) const
Install an ns3::OnOffApplication on each node of the input container configured with all the attribut...
Include Radiotap link layer information.
Definition: wifi-helper.h:117
void SetAttribute(std::string name, const AttributeValue &value)
Helper function used to set the underlying application attributes.
void SetBase(Ipv4Address network, Ipv4Mask mask, Ipv4Address base="0.0.0.1")
Set the base network number, network mask and base address.
Ipv4Address GetAddress(uint32_t i, uint32_t j=0) const