A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
wifi-simple-interference.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2009 The Boeing Company
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 */
7
8// This script configures three nodes on an 802.11b physical layer, with
9// 802.11b NICs in adhoc mode. There is a transmitter, receiver, and
10// interferer. The transmitter sends one packet to the receiver and
11// the receiver receives it with a certain configurable RSS (by default,
12// -80 dBm). The interferer does not do carrier sense and also sends
13// the packet to interfere with the primary packet. The channel model
14// is clear channel.
15//
16// Therefore, at the receiver, the reception looks like this:
17//
18// ------------------time---------------->
19// t0
20//
21// |------------------------------------|
22// | |
23// | primary received frame (time t0) |
24// | |
25// |------------------------------------|
26//
27//
28// t1
29// |-----------------------------------|
30// | |
31// | interfering frame (time t1) |
32// | |
33// |-----------------------------------|
34//
35// The orientation is:
36// n2 ---------> n0 <---------- n1
37// interferer receiver transmitter
38//
39// The configurable parameters are:
40// - Prss (primary rss) (-80 dBm default)
41// - Irss (interfering rss) (-95 dBm default)
42// - delta (t1-t0, may be negative, default 0ns)
43// - PpacketSize (primary packet size) (bytes, default 1000)
44// - IpacketSize (interferer packet size) (bytes, default 1000)
45//
46// For instance, for this configuration, the interfering frame arrives
47// at -90 dBm with a time offset of 3.2 microseconds:
48//
49// ./ns3 run "wifi-simple-interference --Irss=-90 --delta=3.2us"
50//
51// Note that all ns-3 attributes (not just the ones exposed in the below
52// script) can be changed at command line; see the documentation.
53//
54// This script can also be helpful to put the Wifi layer into verbose
55// logging mode; this command will turn on all wifi logging:
56//
57// ./ns3 run "wifi-simple-interference --verbose=1"
58//
59// When you are done, you will notice a pcap trace file in your directory.
60// If you have tcpdump installed, you can try this:
61//
62// tcpdump -r wifi-simple-interference-0-0.pcap -nn -tt
63// reading from file wifi-simple-interference-0-0.pcap, link-type IEEE802_11_RADIO (802.11 plus
64// radiotap header) 10.008754 13308699848833236992us tsft fragmented 0.0 Mb/s 0 MHz 16dBm signal
65// 2dBm noise IP 10.0.0.2.49153 > 255.255.255.255.80: UDP, length 1000
66//
67// With a zero delta time offset, only the first packet will be decoded; the second packet
68// transmission must be delayed past the end of the first packet to receive it..
69//
70// Next, try this command and look at the tcpdump-- you should see two packets
71// that are no longer interfering:
72// ./ns3 run "wifi-simple-interference --delta=9000us"
73
74#include "ns3/command-line.h"
75#include "ns3/config.h"
76#include "ns3/double.h"
77#include "ns3/internet-stack-helper.h"
78#include "ns3/ipv4-address-helper.h"
79#include "ns3/log.h"
80#include "ns3/mobility-helper.h"
81#include "ns3/mobility-model.h"
82#include "ns3/ssid.h"
83#include "ns3/string.h"
84#include "ns3/yans-wifi-channel.h"
85#include "ns3/yans-wifi-helper.h"
86
87using namespace ns3;
88
89NS_LOG_COMPONENT_DEFINE("WifiSimpleInterference");
90
91/**
92 * Print a packer that has been received.
93 *
94 * @param socket The receiving socket.
95 * @return a string with the packet details.
96 */
97static inline std::string
99{
100 Address addr;
101
102 std::ostringstream oss;
103
104 while (socket->Recv())
105 {
106 socket->GetSockName(addr);
108
109 oss << "Received one packet! Socket: " << iaddr.GetIpv4() << " port: " << iaddr.GetPort();
110 }
111
112 return oss.str();
113}
114
115/**
116 * Function called when a packet is received.
117 *
118 * @param socket The receiving socket.
119 */
120static void
125
126/**
127 * Generate traffic
128 *
129 * @param socket The sending socket.
130 * @param pktSize The packet size.
131 */
132static void
134{
135 socket->Send(Create<Packet>(pktSize));
136}
137
138int
139main(int argc, char* argv[])
140{
141 std::string phyMode{"DsssRate1Mbps"};
142 dBm_u Prss{-80};
143 dBm_u Irss{-95};
144 Time delta{"0ns"};
145 uint32_t PpacketSize{1000}; // bytes
146 uint32_t IpacketSize{1000}; // bytes
147 bool verbose{false};
148
149 // these are not command line arguments for this version
150 Time startTime{"10s"};
151 meter_u distanceToRx{100.0}; // If you change this, also change TxGain below
152
153 CommandLine cmd(__FILE__);
154 cmd.AddValue("phyMode", "Wifi Phy mode", phyMode);
155 cmd.AddValue("Prss", "Intended primary received signal strength (dBm)", Prss);
156 cmd.AddValue("Irss", "Intended interfering received signal strength (dBm)", Irss);
157 cmd.AddValue("delta", "time offset for interfering signal", delta);
158 cmd.AddValue("PpacketSize", "size of application packet sent", PpacketSize);
159 cmd.AddValue("IpacketSize", "size of interfering packet sent", IpacketSize);
160 cmd.AddValue("verbose", "turn on all WifiNetDevice log components", verbose);
161 cmd.Parse(argc, argv);
162
163 // Fix non-unicast data rate to be the same as that of unicast
164 Config::SetDefault("ns3::WifiRemoteStationManager::NonUnicastMode", StringValue(phyMode));
165
167 c.Create(3);
168
169 // The below set of helpers will help us to put together the wifi NICs we want
171 if (verbose)
172 {
173 WifiHelper::EnableLogComponents(); // Turn on all Wifi logging
174 }
175 wifi.SetStandard(WIFI_STANDARD_80211b);
176
177 YansWifiPhyHelper wifiPhy;
178
179 // ns-3 supports RadioTap and Prism tracing extensions for 802.11b
181
182 // Disable preamble detection model to receive signals below -82 dBm
184
185 YansWifiChannelHelper wifiChannel;
186 wifiChannel.SetPropagationDelay("ns3::ConstantSpeedPropagationDelayModel");
187 wifiChannel.AddPropagationLoss("ns3::LogDistancePropagationLossModel");
188 wifiPhy.SetChannel(wifiChannel.Create());
189
190 // Add a mac and disable rate control
191 WifiMacHelper wifiMac;
192 wifi.SetRemoteStationManager("ns3::ConstantRateWifiManager",
193 "DataMode",
194 StringValue(phyMode),
195 "ControlMode",
196 StringValue(phyMode));
197 // Set it to adhoc mode
198 wifiMac.SetType("ns3::AdhocWifiMac");
199 NetDeviceContainer devices = wifi.Install(wifiPhy, wifiMac, c.Get(0));
200 // Setting RxSensitivity to 0 dBm will disable the two sending devices from detecting
201 // received signals, so that they do not back off
202 wifiPhy.Set("RxSensitivity", DoubleValue(0));
203 // We use the TxGain parameter on each sender to control the received signal power.
204 // The transmit power is roughly 16 dBm. The signal attenuation from both senders to
205 // the receiving device is 106.7 dB (100 meters at this frequency, based on the
206 // LogDistancePropagationLossModel). We want the receive signal strength to be
207 // Prss dBm. We therefore want to solve for the gain as follows:
208 // 16 dBm + TxGain - propagationLoss = Prss (dBm)
209 // Working backwards, TxGain = Prss (dBm) - 16 dB + 106.7 dB = Prss (dBm) + 90.7 dB
210 dB_u powerOffset{90.7};
211 wifiPhy.Set("TxGain", DoubleValue(Prss + powerOffset));
212 devices.Add(wifi.Install(wifiPhy, wifiMac, c.Get(1)));
213 // Repeat for the interferer
214 wifiPhy.Set("TxGain", DoubleValue(Irss + powerOffset));
215 devices.Add(wifi.Install(wifiPhy, wifiMac, c.Get(2)));
216
219 positionAlloc->Add(Vector(0.0, 0.0, 0.0));
220 positionAlloc->Add(Vector(distanceToRx, 0.0, 0.0));
221 positionAlloc->Add(Vector(-1 * distanceToRx, 0.0, 0.0));
222 mobility.SetPositionAllocator(positionAlloc);
223 mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");
224 mobility.Install(c);
225
227 internet.Install(c);
228
230 address.SetBase("10.0.0.0", "255.255.255.0");
231 Ipv4InterfaceContainer ipInterfaces;
232 ipInterfaces = address.Assign(devices);
233
234 TypeId tid = TypeId::LookupByName("ns3::UdpSocketFactory");
235 Ptr<Socket> recvSink = Socket::CreateSocket(c.Get(0), tid);
236 InetSocketAddress local = InetSocketAddress(Ipv4Address("10.1.1.1"), 80);
237 recvSink->Bind(local);
238 recvSink->SetRecvCallback(MakeCallback(&ReceivePacket));
239
240 Ptr<Socket> source = Socket::CreateSocket(c.Get(1), tid);
241 InetSocketAddress remote = InetSocketAddress(Ipv4Address("255.255.255.255"), 80);
242 source->SetAllowBroadcast(true);
243 source->Connect(remote);
244
245 // Interferer will send to a different port; we will not see a
246 // "Received packet" message
247 Ptr<Socket> interferer = Socket::CreateSocket(c.Get(2), tid);
248 InetSocketAddress interferingAddr = InetSocketAddress(Ipv4Address("255.255.255.255"), 49000);
249 interferer->SetAllowBroadcast(true);
250 interferer->Connect(interferingAddr);
251
252 // Tracing
253 wifiPhy.EnablePcap("wifi-simple-interference", devices.Get(0));
254
255 // Output what we are doing
256 NS_LOG_UNCOND("Primary packet RSS=" << Prss << " dBm and interferer RSS=" << Irss
257 << " dBm at time offset=" << delta.As(Time::US));
258
259 Simulator::ScheduleWithContext(source->GetNode()->GetId(),
260 startTime,
262 source,
263 PpacketSize);
264
265 Simulator::ScheduleWithContext(interferer->GetNode()->GetId(),
266 startTime + delta,
268 interferer,
269 IpacketSize);
270
273
274 return 0;
275}
a polymophic address class
Definition address.h:114
Parse command-line arguments.
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition double.h:31
an Inet address class
Ipv4Address GetIpv4() const
static InetSocketAddress ConvertFrom(const Address &address)
Returns an InetSocketAddress which corresponds to the input Address.
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.
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.
void EnablePcap(std::string prefix, Ptr< NetDevice > nd, bool promiscuous=false, bool explicitFilename=false)
Enable pcap output the indicated net device.
Smart pointer class similar to boost::intrusive_ptr.
Definition ptr.h:70
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:125
static void ScheduleWithContext(uint32_t context, const Time &delay, FUNC f, Ts &&... args)
Schedule an event with the given context.
Definition simulator.h:597
static void Run()
Run the simulation.
Definition simulator.cc:161
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:61
Hold variables of type string.
Definition string.h:45
Simulation virtual time values and global simulation resolution.
Definition nstime.h:95
@ US
microsecond
Definition nstime.h:108
a unique identifier for an interface.
Definition type-id.h:50
static TypeId LookupByName(std::string name)
Get a TypeId by name.
Definition type-id.cc:870
helps to create WifiNetDevice objects
static void EnableLogComponents(LogLevel logLevel=LOG_LEVEL_ALL)
Helper to enable all WifiNetDevice log components with one statement.
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.
void Set(std::string name, const AttributeValue &v)
void DisablePreambleDetectionModel()
Disable the preamble detection model on all links.
@ DLT_IEEE802_11_RADIO
Include Radiotap link layer information.
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 ReceivePacket(Ptr< Socket > socket)
static void GenerateTraffic(Ptr< Socket > socket, uint32_t pktSize, Ptr< Node > n, uint32_t pktCount, Time pktInterval)
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:690
void SetDefault(std::string name, const AttributeValue &value)
Definition config.cc:886
#define NS_LOG_UNCOND(msg)
Output the requested message unconditionally.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:194
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:627
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition ptr.h:454
@ WIFI_STANDARD_80211b
address
Definition first.py:36
devices
Definition first.py:31
Every class exported by the ns3 library is enclosed in the ns3 namespace.
double dBm_u
dBm weak type
Definition wifi-units.h:27
double dB_u
dB weak type
Definition wifi-units.h:28
double meter_u
meter weak type
Definition wifi-units.h:32
wifi
Definition third.py:84
mobility
Definition third.py:92
bool verbose
static void ReceivePacket(Ptr< Socket > socket)
Function called when a packet is received.
static std::string PrintReceivedPacket(Ptr< Socket > socket)
Print a packer that has been received.
static void GenerateTraffic(Ptr< Socket > socket, uint32_t pktSize)
Generate traffic.