A Discrete-Event Network Simulator
API
ht-wifi-network.cc
Go to the documentation of this file.
1 /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2009 MIRKO BANCHI
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  * Authors: Mirko Banchi <mk.banchi@gmail.com>
19  * Sebastien Deronne <sebastien.deronne@gmail.com>
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 is a simple example of an IEEE 802.11n Wi-Fi network.
30 //
31 //Network topology:
32 //
33 // Wifi 192.168.1.0
34 //
35 // AP
36 // * *
37 // | |
38 // n1 n2
39 //
40 //Packets in this simulation aren't marked with a QosTag so they are considered
41 //belonging to BestEffort Access Class (AC_BE).
42 
43 using namespace ns3;
44 
45 NS_LOG_COMPONENT_DEFINE ("ht-wifi-network");
46 
47 int main (int argc, char *argv[])
48 {
49  bool udp = true;
50  double simulationTime = 10; //seconds
51  CommandLine cmd;
52  cmd.AddValue ("simulationTime", "Simulation time in seconds", simulationTime);
53  cmd.AddValue ("udp", "UDP if set to 1, TCP otherwise", udp);
54  cmd.Parse (argc,argv);
55 
56  std::cout << "DataRate" << "\t" << "Throughput" << '\n';
57  for (int i = 0; i <= 31; i++)
58  {
59  uint32_t payloadSize; //1500 byte IP packet
60  if (udp)
61  {
62  payloadSize = 1472; //bytes
63  }
64  else
65  {
66  payloadSize = 1448; //bytes
67  Config::SetDefault ("ns3::TcpSocket::SegmentSize", UintegerValue (payloadSize));
68  }
69 
70  NodeContainer wifiStaNode;
71  wifiStaNode.Create (1);
72  NodeContainer wifiApNode;
73  wifiApNode.Create (1);
74 
77  phy.SetChannel (channel.Create ());
78 
79  if (i <= 7)
80  {
81  phy.Set ("ShortGuardEnabled", BooleanValue (false));
82  phy.Set ("ChannelBonding", BooleanValue (false));
83  }
84  else if (i > 7 && i <= 15)
85  {
86  phy.Set ("ShortGuardEnabled", BooleanValue (true));
87  phy.Set ("ChannelBonding", BooleanValue (false));
88  }
89  else if (i > 15 && i <= 23)
90  {
91  phy.Set ("ShortGuardEnabled", BooleanValue (false));
92  phy.Set ("ChannelBonding", BooleanValue (true));
93  }
94  else
95  {
96  phy.Set ("ShortGuardEnabled", BooleanValue (true));
97  phy.Set ("ChannelBonding", BooleanValue (true));
98  }
99 
103 
104  Ssid ssid = Ssid ("ns380211n");
105 
106  double datarate = 0;
108  if (i == 0)
109  {
110  DataRate = StringValue ("OfdmRate6_5MbpsBW20MHz");
111  datarate = 6.5;
112  }
113  else if (i == 1)
114  {
115  DataRate = StringValue ("OfdmRate13MbpsBW20MHz");
116  datarate = 13;
117  }
118  else if (i == 2)
119  {
120  DataRate = StringValue ("OfdmRate19_5MbpsBW20MHz");
121  datarate = 19.5;
122  }
123  else if (i == 3)
124  {
125  DataRate = StringValue ("OfdmRate26MbpsBW20MHz");
126  datarate = 26;
127  }
128  else if (i == 4)
129  {
130  DataRate = StringValue ("OfdmRate39MbpsBW20MHz");
131  datarate = 39;
132  }
133  else if (i == 5)
134  {
135  DataRate = StringValue ("OfdmRate52MbpsBW20MHz");
136  datarate = 52;
137  }
138  else if (i == 6)
139  {
140  DataRate = StringValue ("OfdmRate58_5MbpsBW20MHz");
141  datarate = 58.5;
142  }
143  else if (i == 7)
144  {
145  DataRate = StringValue ("OfdmRate65MbpsBW20MHz");
146  datarate = 65;
147  }
148  else if (i == 8)
149  {
150  DataRate = StringValue ("OfdmRate7_2MbpsBW20MHz");
151  datarate = 7.2;
152  }
153  else if (i == 9)
154  {
155  DataRate = StringValue ("OfdmRate14_4MbpsBW20MHz");
156  datarate = 14.4;
157  }
158  else if (i == 10)
159  {
160  DataRate = StringValue ("OfdmRate21_7MbpsBW20MHz");
161  datarate = 21.7;
162  }
163  else if (i == 11)
164  {
165  DataRate = StringValue ("OfdmRate28_9MbpsBW20MHz");
166  datarate = 28.9;
167  }
168  else if (i == 12)
169  {
170  DataRate = StringValue ("OfdmRate43_3MbpsBW20MHz");
171  datarate = 43.3;
172  }
173  else if (i == 13)
174  {
175  DataRate = StringValue ("OfdmRate57_8MbpsBW20MHz");
176  datarate = 57.8;
177  }
178  else if (i == 14)
179  {
180  DataRate = StringValue ("OfdmRate65MbpsBW20MHzShGi");
181  datarate = 65;
182  }
183  else if (i == 15)
184  {
185  DataRate = StringValue ("OfdmRate72_2MbpsBW20MHz");
186  datarate = 72.2;
187  }
188  else if (i == 16)
189  {
190  DataRate = StringValue ("OfdmRate13_5MbpsBW40MHz");
191  datarate = 13.5;
192  }
193  else if (i == 17)
194  {
195  DataRate = StringValue ("OfdmRate27MbpsBW40MHz");
196  datarate = 27;
197  }
198  else if (i == 18)
199  {
200  DataRate = StringValue ("OfdmRate40_5MbpsBW40MHz");
201  datarate = 40.5;
202  }
203  else if (i == 19)
204  {
205  DataRate = StringValue ("OfdmRate54MbpsBW40MHz");
206  datarate = 54;
207  }
208  else if (i == 20)
209  {
210  DataRate = StringValue ("OfdmRate81MbpsBW40MHz");
211  datarate = 81;
212  }
213  else if (i == 21)
214  {
215  DataRate = StringValue ("OfdmRate108MbpsBW40MHz");
216  datarate = 108;
217  }
218  else if (i == 22)
219  {
220  DataRate = StringValue ("OfdmRate121_5MbpsBW40MHz");
221  datarate = 121.5;
222  }
223  else if (i == 23)
224  {
225  DataRate = StringValue ("OfdmRate135MbpsBW40MHz");
226  datarate = 135;
227  }
228  else if (i == 24)
229  {
230  DataRate = StringValue ("OfdmRate15MbpsBW40MHz");
231  datarate = 15;
232  }
233  else if (i == 25)
234  {
235  DataRate = StringValue ("OfdmRate30MbpsBW40MHz");
236  datarate = 30;
237  }
238  else if (i == 26)
239  {
240  DataRate = StringValue ("OfdmRate45MbpsBW40MHz");
241  datarate = 45;
242  }
243  else if (i == 27)
244  {
245  DataRate = StringValue ("OfdmRate60MbpsBW40MHz");
246  datarate = 60;
247  }
248  else if (i == 28)
249  {
250  DataRate = StringValue ("OfdmRate90MbpsBW40MHz");
251  datarate = 90;
252  }
253  else if (i == 29)
254  {
255  DataRate = StringValue ("OfdmRate120MbpsBW40MHz");
256  datarate = 120;
257  }
258  else if (i == 30)
259  {
260  DataRate = StringValue ("OfdmRate135MbpsBW40MHzShGi");
261  datarate = 135;
262  }
263  else
264  {
265  DataRate = StringValue ("OfdmRate150MbpsBW40MHz");
266  datarate = 150;
267  }
268 
269  wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager","DataMode", DataRate,
270  "ControlMode", DataRate);
271  mac.SetType ("ns3::StaWifiMac",
272  "Ssid", SsidValue (ssid),
273  "ActiveProbing", BooleanValue (false));
274 
275  NetDeviceContainer staDevice;
276  staDevice = wifi.Install (phy, mac, wifiStaNode);
277 
278  mac.SetType ("ns3::ApWifiMac",
279  "Ssid", SsidValue (ssid));
280 
281  NetDeviceContainer apDevice;
282  apDevice = wifi.Install (phy, mac, wifiApNode);
283 
284  // mobility.
285  MobilityHelper mobility;
286  Ptr<ListPositionAllocator> positionAlloc = CreateObject<ListPositionAllocator> ();
287 
288  positionAlloc->Add (Vector (0.0, 0.0, 0.0));
289  positionAlloc->Add (Vector (1.0, 0.0, 0.0));
290  mobility.SetPositionAllocator (positionAlloc);
291 
292  mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
293 
294  mobility.Install (wifiApNode);
295  mobility.Install (wifiStaNode);
296 
297  /* Internet stack*/
299  stack.Install (wifiApNode);
300  stack.Install (wifiStaNode);
301 
303 
304  address.SetBase ("192.168.1.0", "255.255.255.0");
305  Ipv4InterfaceContainer staNodeInterface;
306  Ipv4InterfaceContainer apNodeInterface;
307 
308  staNodeInterface = address.Assign (staDevice);
309  apNodeInterface = address.Assign (apDevice);
310 
311  /* Setting applications */
312  ApplicationContainer serverApp, sinkApp;
313  if (udp)
314  {
315  //UDP flow
316  UdpServerHelper myServer (9);
317  serverApp = myServer.Install (wifiStaNode.Get (0));
318  serverApp.Start (Seconds (0.0));
319  serverApp.Stop (Seconds (simulationTime+1));
320 
321  UdpClientHelper myClient (staNodeInterface.GetAddress (0), 9);
322  myClient.SetAttribute ("MaxPackets", UintegerValue (4294967295u));
323  myClient.SetAttribute ("Interval", TimeValue (Time ("0.00002"))); //packets/s
324  myClient.SetAttribute ("PacketSize", UintegerValue (payloadSize));
325 
326  ApplicationContainer clientApp = myClient.Install (wifiApNode.Get (0));
327  clientApp.Start (Seconds (1.0));
328  clientApp.Stop (Seconds (simulationTime+1));
329  }
330  else
331  {
332  //TCP flow
333  uint16_t port = 50000;
334  Address apLocalAddress (InetSocketAddress (Ipv4Address::GetAny (), port));
335  PacketSinkHelper packetSinkHelper ("ns3::TcpSocketFactory", apLocalAddress);
336  sinkApp = packetSinkHelper.Install (wifiStaNode.Get (0));
337 
338  sinkApp.Start (Seconds (0.0));
339  sinkApp.Stop (Seconds (simulationTime+1));
340 
341  OnOffHelper onoff ("ns3::TcpSocketFactory",Ipv4Address::GetAny ());
342  onoff.SetAttribute ("OnTime", StringValue ("ns3::ConstantRandomVariable[Constant=1]"));
343  onoff.SetAttribute ("OffTime", StringValue ("ns3::ConstantRandomVariable[Constant=0]"));
344  onoff.SetAttribute ("PacketSize", UintegerValue (payloadSize));
345  onoff.SetAttribute ("DataRate", DataRateValue (1000000000)); //bit/s
347 
348  AddressValue remoteAddress (InetSocketAddress (staNodeInterface.GetAddress (0), port));
349  onoff.SetAttribute ("Remote", remoteAddress);
350  apps.Add (onoff.Install (wifiApNode.Get (0)));
351  apps.Start (Seconds (1.0));
352  apps.Stop (Seconds (simulationTime+1));
353  }
354 
356 
357  Simulator::Stop (Seconds (simulationTime+1));
358  Simulator::Run ();
360 
361  double throughput = 0;
362  if (udp)
363  {
364  //UDP
365  uint32_t totalPacketsThrough = DynamicCast<UdpServer> (serverApp.Get (0))->GetReceived ();
366  throughput = totalPacketsThrough * payloadSize * 8 / (simulationTime * 1000000.0); //Mbit/s
367  }
368  else
369  {
370  //TCP
371  uint32_t totalPacketsThrough = DynamicCast<PacketSink> (sinkApp.Get (0))->GetTotalRx ();
372  throughput = totalPacketsThrough * 8 / (simulationTime * 1000000.0); //Mbit/s
373  }
374  std::cout << datarate << "\t\t" << throughput << " Mbit/s" << std::endl;
375  }
376  return 0;
377 }
holds a vector of ns3::Application pointers.
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
create HT-enabled MAC layers for a ns3::WifiNetDevice.
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:73
static void PopulateRoutingTables(void)
Build a routing database and initialize the routing tables of the nodes in the simulation.
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. ...
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())
Set the underlying type of the MAC and its attributes.
static void Run(void)
Run the simulation.
Definition: simulator.cc:200
#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...
void Set(std::string name, const AttributeValue &v)
static YansWifiPhyHelper Default(void)
Create a phy helper in a default working state.
helps to create WifiNetDevice objects
Definition: wifi-helper.h:92
A helper to make it easier to instantiate an ns3::OnOffApplication on a set of nodes.
Definition: on-off-helper.h:42
virtual NetDeviceContainer Install(const WifiPhyHelper &phy, const WifiMacHelper &mac, NodeContainer c) const
Definition: wifi-helper.cc:102
uint16_t port
Definition: dsdv-manet.cc:44
a polymophic address class
Definition: address.h:90
static HtWifiMacHelper Default(void)
Create a mac helper in a default working state.
Class for representing data rates.
Definition: data-rate.h:88
void SetChannel(Ptr< YansWifiChannel > channel)
void Install(Ptr< Node > node) const
"Layout" a single node according to the current position allocator type.
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:928
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:96
Create a server application which waits for input UDP packets and uses the information carried into t...
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:201
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:164
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.
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...
manage and create wifi channel objects for the yans model.
tuple stack
Definition: first.py:34
The IEEE 802.11 SSID Information Element.
Definition: ssid.h:37
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...
AttributeValue implementation for DataRate.
Definition: data-rate.h:238
void AddValue(const std::string &name, const std::string &help, T &value)
Add a program argument, assigning to POD.
Definition: command-line.h:491
static void Stop(void)
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:208
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:866
AttributeValue implementation for Ssid.
Definition: ssid.h:93
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:683
void Add(Vector v)
Add a position to the list of positions.
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.
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...
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
static WifiHelper Default(void)
Definition: wifi-helper.cc:65