A Discrete-Event Network Simulator
QKDNetSim v2.0 (NS-3 v3.41) @ (+)
API
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
wifi-simple-infra.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2009 The Boeing Company
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation;
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  *
17  */
18 
19 // This script configures two nodes on an 802.11b physical layer, with
20 // 802.11b NICs in infrastructure mode, and by default, the station sends
21 // one packet of 1000 (application) bytes to the access point. Unlike
22 // the default physical layer configuration in which the path loss increases
23 // (and the received signal strength decreases) as the distance between the
24 // nodes increases, this example uses an artificial path loss model that
25 // allows the configuration of the received signal strength (RSS) regardless
26 // of other transmitter parameters (such as transmit power) or distance.
27 // Therefore, changing position of the nodes has no effect.
28 //
29 // There are a number of command-line options available to control
30 // the default behavior. The list of available command-line options
31 // can be listed with the following command:
32 // ./ns3 run "wifi-simple-infra --help"
33 // Additional command-line options are available via the generic attribute
34 // configuration system.
35 //
36 // For instance, for the default configuration, the physical layer will
37 // stop successfully receiving packets when rss drops to -82 dBm or below.
38 // To see this effect, try running:
39 //
40 // ./ns3 run "wifi-simple-infra --rss=-80 --numPackets=20"
41 // ./ns3 run "wifi-simple-infra --rss=-81 --numPackets=20"
42 // ./ns3 run "wifi-simple-infra --rss=-82 --numPackets=20"
43 //
44 // The last command (and any RSS value lower than this) results in no
45 // packets received. This is due to the preamble detection model that
46 // dominates the reception performance. By default, the
47 // ThresholdPreambleDetectionModel is added to all WifiPhy objects, and this
48 // model prevents reception unless the incoming signal has a RSS above its
49 // 'MinimumRssi' value (default of -82 dBm) and has a SNR above the
50 // 'Threshold' value (default of 4).
51 //
52 // If we relax these values, we can instead observe that signal reception
53 // due to the 802.11b error model alone is much lower. For instance,
54 // setting the MinimumRssi to -101 (around the thermal noise floor).
55 // and the SNR Threshold to -10 dB, shows that the DsssErrorRateModel can
56 // successfully decode at RSS values of -97 or -98 dBm.
57 //
58 // ./ns3 run "wifi-simple-infra --rss=-97 --numPackets=20
59 // --ns3::ThresholdPreambleDetectionModel::Threshold=-10
60 // --ns3::ThresholdPreambleDetectionModel::MinimumRssi=-101"
61 // ./ns3 run "wifi-simple-infra --rss=-98 --numPackets=20
62 // --ns3::ThresholdPreambleDetectionModel::Threshold=-10
63 // --ns3::ThresholdPreambleDetectionModel::MinimumRssi=-101"
64 // ./ns3 run "wifi-simple-infra --rss=-99 --numPackets=20
65 // --ns3::ThresholdPreambleDetectionModel::Threshold=-10
66 // --ns3::ThresholdPreambleDetectionModel::MinimumRssi=-101"
67 
68 //
69 // Note that all ns-3 attributes (not just the ones exposed in the below
70 // script) can be changed at command line; see the documentation.
71 //
72 // This script can also be helpful to put the Wifi layer into verbose
73 // logging mode; this command will turn on all wifi logging:
74 //
75 // ./ns3 run "wifi-simple-infra --verbose=1"
76 //
77 // When you are done, you will notice two pcap trace files in your directory.
78 // If you have tcpdump installed, you can try this:
79 //
80 // tcpdump -r wifi-simple-infra-0-0.pcap -nn -tt
81 //
82 
83 #include "ns3/command-line.h"
84 #include "ns3/config.h"
85 #include "ns3/double.h"
86 #include "ns3/internet-stack-helper.h"
87 #include "ns3/ipv4-address-helper.h"
88 #include "ns3/log.h"
89 #include "ns3/mobility-helper.h"
90 #include "ns3/mobility-model.h"
91 #include "ns3/ssid.h"
92 #include "ns3/string.h"
93 #include "ns3/yans-wifi-channel.h"
94 #include "ns3/yans-wifi-helper.h"
95 
96 using namespace ns3;
97 
98 NS_LOG_COMPONENT_DEFINE("WifiSimpleInfra");
99 
105 void
107 {
108  while (socket->Recv())
109  {
110  std::cout << "Received one packet!" << std::endl;
111  }
112 }
113 
122 static void
123 GenerateTraffic(Ptr<Socket> socket, uint32_t pktSize, uint32_t pktCount, Time pktInterval)
124 {
125  if (pktCount > 0)
126  {
127  NS_LOG_INFO("Generating one packet of size " << pktSize);
128  socket->Send(Create<Packet>(pktSize));
129  Simulator::Schedule(pktInterval,
131  socket,
132  pktSize,
133  pktCount - 1,
134  pktInterval);
135  }
136  else
137  {
138  socket->Close();
139  }
140 }
141 
142 int
143 main(int argc, char* argv[])
144 {
145  std::string phyMode("DsssRate1Mbps");
146  double rss = -80; // -dBm
147  uint32_t packetSize = 1000; // bytes
148  uint32_t numPackets = 1;
149  Time interval = Seconds(1.0);
150  bool verbose = false;
151 
152  CommandLine cmd(__FILE__);
153  cmd.AddValue("phyMode", "Wifi Phy mode", phyMode);
154  cmd.AddValue("rss", "received signal strength", rss);
155  cmd.AddValue("packetSize", "size of application packet sent", packetSize);
156  cmd.AddValue("numPackets", "number of packets generated", numPackets);
157  cmd.AddValue("interval", "interval between packets", interval);
158  cmd.AddValue("verbose", "turn on all WifiNetDevice log components", verbose);
159  cmd.Parse(argc, argv);
160 
161  // Fix non-unicast data rate to be the same as that of unicast
162  Config::SetDefault("ns3::WifiRemoteStationManager::NonUnicastMode", StringValue(phyMode));
163 
164  NodeContainer c;
165  c.Create(2);
166 
167  // The below set of helpers will help us to put together the wifi NICs we want
169  if (verbose)
170  {
171  WifiHelper::EnableLogComponents(); // Turn on all Wifi logging
172  }
173  wifi.SetStandard(WIFI_STANDARD_80211b);
174 
175  YansWifiPhyHelper wifiPhy;
176  // This is one parameter that matters when using FixedRssLossModel
177  // set it to zero; otherwise, gain will be added
178  wifiPhy.Set("RxGain", DoubleValue(0));
179  // ns-3 supports RadioTap and Prism tracing extensions for 802.11b
181 
182  YansWifiChannelHelper wifiChannel;
183  wifiChannel.SetPropagationDelay("ns3::ConstantSpeedPropagationDelayModel");
184  // The below FixedRssLossModel will cause the rss to be fixed regardless
185  // of the distance between the two stations, and the transmit power
186  wifiChannel.AddPropagationLoss("ns3::FixedRssLossModel", "Rss", DoubleValue(rss));
187  wifiPhy.SetChannel(wifiChannel.Create());
188 
189  // Add a mac and disable rate control
190  WifiMacHelper wifiMac;
191  wifi.SetRemoteStationManager("ns3::ConstantRateWifiManager",
192  "DataMode",
193  StringValue(phyMode),
194  "ControlMode",
195  StringValue(phyMode));
196 
197  // Setup the rest of the MAC
198  Ssid ssid = Ssid("wifi-default");
199  // setup STA
200  wifiMac.SetType("ns3::StaWifiMac", "Ssid", SsidValue(ssid));
201  NetDeviceContainer staDevice = wifi.Install(wifiPhy, wifiMac, c.Get(0));
202  NetDeviceContainer devices = staDevice;
203  // setup AP
204  wifiMac.SetType("ns3::ApWifiMac", "Ssid", SsidValue(ssid));
205  NetDeviceContainer apDevice = wifi.Install(wifiPhy, wifiMac, c.Get(1));
206  devices.Add(apDevice);
207 
208  // Note that with FixedRssLossModel, the positions below are not
209  // used for received signal strength.
211  Ptr<ListPositionAllocator> positionAlloc = CreateObject<ListPositionAllocator>();
212  positionAlloc->Add(Vector(0.0, 0.0, 0.0));
213  positionAlloc->Add(Vector(5.0, 0.0, 0.0));
214  mobility.SetPositionAllocator(positionAlloc);
215  mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");
216  mobility.Install(c);
217 
219  internet.Install(c);
220 
222  ipv4.SetBase("10.1.1.0", "255.255.255.0");
223  Ipv4InterfaceContainer i = ipv4.Assign(devices);
224 
225  TypeId tid = TypeId::LookupByName("ns3::UdpSocketFactory");
226  Ptr<Socket> recvSink = Socket::CreateSocket(c.Get(0), tid);
228  recvSink->Bind(local);
230 
231  Ptr<Socket> source = Socket::CreateSocket(c.Get(1), tid);
232  InetSocketAddress remote = InetSocketAddress(Ipv4Address("255.255.255.255"), 80);
233  source->SetAllowBroadcast(true);
234  source->Connect(remote);
235 
236  // Tracing
237  wifiPhy.EnablePcap("wifi-simple-infra", devices);
238 
239  // Output what we are doing
240  std::cout << "Testing " << numPackets << " packets sent with receiver rss " << rss << std::endl;
241 
243  Seconds(1.0),
245  source,
246  packetSize,
247  numPackets,
248  interval);
249 
250  Simulator::Stop(Seconds(30.0));
251  Simulator::Run();
253 
254  return 0;
255 }
Parse command-line arguments.
Definition: command-line.h:232
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition: double.h:42
an Inet address class
aggregate IP/TCP/UDP functionality to existing Nodes.
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:42
static Ipv4Address GetAny()
holds a vector of std::pair of Ptr<Ipv4> and interface index.
Helper class used to assign positions and mobility models to nodes.
holds a vector of ns3::NetDevice pointers
keep track of a set of node pointers.
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
uint32_t GetId() const
Definition: node.cc:117
void EnablePcap(std::string prefix, Ptr< NetDevice > nd, bool promiscuous=false, bool explicitFilename=false)
Enable pcap output the indicated net device.
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:571
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:142
static void ScheduleWithContext(uint32_t context, const Time &delay, FUNC f, Ts &&... args)
Schedule an event with the given context.
Definition: simulator.h:588
static void Run()
Run the simulation.
Definition: simulator.cc:178
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:186
virtual int Send(Ptr< Packet > p, uint32_t flags)=0
Send data (or dummy data) to the remote host.
virtual bool SetAllowBroadcast(bool allowBroadcast)=0
Configure whether broadcast datagram transmissions are allowed.
void SetRecvCallback(Callback< void, Ptr< Socket >> receivedData)
Notify application when new data is available to be read.
Definition: socket.cc:128
virtual Ptr< Packet > Recv(uint32_t maxSize, uint32_t flags)=0
Read data from the socket.
virtual int Connect(const Address &address)=0
Initiate a connection to a remote host.
virtual Ptr< Node > GetNode() const =0
Return the node this socket is associated with.
static Ptr< Socket > CreateSocket(Ptr< Node > node, TypeId tid)
This method wraps the creation of sockets that is performed on a given node by a SocketFactory specif...
Definition: socket.cc:72
virtual int Close()=0
Close a socket.
virtual int Bind(const Address &address)=0
Allocate a local endpoint for this socket.
The IEEE 802.11 SSID Information Element.
Definition: ssid.h:36
Hold variables of type string.
Definition: string.h:56
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
a unique identifier for an interface.
Definition: type-id.h:59
static TypeId LookupByName(std::string name)
Get a TypeId by name.
Definition: type-id.cc:835
helps to create WifiNetDevice objects
Definition: wifi-helper.h:324
static void EnableLogComponents()
Helper to enable all WifiNetDevice log components with one statement.
Definition: wifi-helper.cc:880
create MAC layers for a ns3::WifiNetDevice.
void SetType(std::string type, Args &&... args)
void SetPcapDataLinkType(SupportedPcapDataLinkTypes dlt)
Set the data link type of PCAP traces to be used.
Definition: wifi-helper.cc:543
void Set(std::string name, const AttributeValue &v)
Definition: wifi-helper.cc:163
@ DLT_IEEE802_11_RADIO
Include Radiotap link layer information.
Definition: wifi-helper.h:178
manage and create wifi channel objects for the YANS model.
void SetPropagationDelay(std::string name, Ts &&... args)
void AddPropagationLoss(std::string name, Ts &&... args)
Ptr< YansWifiChannel > Create() const
Make it easy to create and manage PHY objects for the YANS model.
void SetChannel(Ptr< YansWifiChannel > channel)
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:890
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:275
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1326
@ WIFI_STANDARD_80211b
devices
Definition: first.py:42
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Callback< R, Args... > MakeCallback(R(T::*memPtr)(Args...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition: callback.h:704
cmd
Definition: second.py:40
ssid
Definition: third.py:93
wifi
Definition: third.py:95
mobility
Definition: third.py:105
bool verbose
uint32_t pktSize
packet size used for the simulation (in bytes)
static const uint32_t packetSize
Packet size generated at the AP.
void ReceivePacket(Ptr< Socket > socket)
Function called when a packet is received.
static void GenerateTraffic(Ptr< Socket > socket, uint32_t pktSize, uint32_t pktCount, Time pktInterval)
Generate traffic.