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.2ns"
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 BSD
64// radio information header) 10.008704 10008704us tsft 1.0 Mb/s 2437 MHz (0x00c0) -80dB signal -98dB
65// noise IP 10.1.1.2.49153 > 10.1.1.255.80: UDP, length 1000
66//
67// Next, try this command and look at the tcpdump-- you should see two packets
68// that are no longer interfering:
69// ./ns3 run "wifi-simple-interference --delta=30000ns"
70
71#include "ns3/command-line.h"
72#include "ns3/config.h"
73#include "ns3/double.h"
74#include "ns3/internet-stack-helper.h"
75#include "ns3/log.h"
76#include "ns3/mobility-helper.h"
77#include "ns3/mobility-model.h"
78#include "ns3/ssid.h"
79#include "ns3/string.h"
80#include "ns3/yans-wifi-channel.h"
81#include "ns3/yans-wifi-helper.h"
82
83using namespace ns3;
84
85NS_LOG_COMPONENT_DEFINE("WifiSimpleInterference");
86
87/**
88 * Print a packer that has been received.
89 *
90 * \param socket The receiving socket.
91 * \return a string with the packet details.
92 */
93static inline std::string
95{
96 Address addr;
97
98 std::ostringstream oss;
99
100 while (socket->Recv())
101 {
102 socket->GetSockName(addr);
104
105 oss << "Received one packet! Socket: " << iaddr.GetIpv4() << " port: " << iaddr.GetPort();
106 }
107
108 return oss.str();
109}
110
111/**
112 * Function called when a packet is received.
113 *
114 * \param socket The receiving socket.
115 */
116static void
121
122/**
123 * Generate traffic
124 *
125 * \param socket The seding socket.
126 * \param pktSize The packet size.
127 * \param pktCount The packet counter.
128 * \param pktInterval The interval between two packets.
129 */
130static void
132{
133 if (pktCount > 0)
134 {
135 socket->Send(Create<Packet>(pktSize));
136 Simulator::Schedule(pktInterval,
138 socket,
139 pktSize,
140 pktCount - 1,
141 pktInterval);
142 }
143 else
144 {
145 socket->Close();
146 }
147}
148
149int
150main(int argc, char* argv[])
151{
152 std::string phyMode{"DsssRate1Mbps"};
153 dBm_u Prss{-80};
154 dBm_u Irss{-95};
155 Time delta{"0ns"};
156 uint32_t PpacketSize{1000}; // bytes
157 uint32_t IpacketSize{1000}; // bytes
158 bool verbose{false};
159
160 // these are not command line arguments for this version
161 uint32_t numPackets{1};
162 Time interPacketInterval{"1s"};
163 Time startTime{"10s"};
164 meter_u distanceToRx{100.0};
165
166 double offset{91}; // This is a magic number used to set the
167 // transmit power, based on other configuration
168 CommandLine cmd(__FILE__);
169 cmd.AddValue("phyMode", "Wifi Phy mode", phyMode);
170 cmd.AddValue("Prss", "Intended primary received signal strength (dBm)", Prss);
171 cmd.AddValue("Irss", "Intended interfering received signal strength (dBm)", Irss);
172 cmd.AddValue("delta", "time offset for interfering signal", delta);
173 cmd.AddValue("PpacketSize", "size of application packet sent", PpacketSize);
174 cmd.AddValue("IpacketSize", "size of interfering packet sent", IpacketSize);
175 cmd.AddValue("verbose", "turn on all WifiNetDevice log components", verbose);
176 cmd.Parse(argc, argv);
177
178 // Fix non-unicast data rate to be the same as that of unicast
179 Config::SetDefault("ns3::WifiRemoteStationManager::NonUnicastMode", StringValue(phyMode));
180
182 c.Create(3);
183
184 // The below set of helpers will help us to put together the wifi NICs we want
186 if (verbose)
187 {
188 WifiHelper::EnableLogComponents(); // Turn on all Wifi logging
189 }
190 wifi.SetStandard(WIFI_STANDARD_80211b);
191
192 YansWifiPhyHelper wifiPhy;
193
194 // ns-3 supports RadioTap and Prism tracing extensions for 802.11b
196
197 YansWifiChannelHelper wifiChannel;
198 wifiChannel.SetPropagationDelay("ns3::ConstantSpeedPropagationDelayModel");
199 wifiChannel.AddPropagationLoss("ns3::LogDistancePropagationLossModel");
200 wifiPhy.SetChannel(wifiChannel.Create());
201
202 // Add a mac and disable rate control
203 WifiMacHelper wifiMac;
204 wifi.SetRemoteStationManager("ns3::ConstantRateWifiManager",
205 "DataMode",
206 StringValue(phyMode),
207 "ControlMode",
208 StringValue(phyMode));
209 // Set it to adhoc mode
210 wifiMac.SetType("ns3::AdhocWifiMac");
211 NetDeviceContainer devices = wifi.Install(wifiPhy, wifiMac, c.Get(0));
212 // This will disable these sending devices from detecting a signal
213 // so that they do not backoff
214 wifiPhy.Set("TxGain", DoubleValue(offset + Prss));
215 devices.Add(wifi.Install(wifiPhy, wifiMac, c.Get(1)));
216 wifiPhy.Set("TxGain", DoubleValue(offset + Irss));
217 devices.Add(wifi.Install(wifiPhy, wifiMac, c.Get(2)));
218
219 // Note that with FixedRssLossModel, the positions below are not
220 // used for received signal strength.
223 positionAlloc->Add(Vector(0.0, 0.0, 0.0));
224 positionAlloc->Add(Vector(distanceToRx, 0.0, 0.0));
225 positionAlloc->Add(Vector(-1 * distanceToRx, 0.0, 0.0));
226 mobility.SetPositionAllocator(positionAlloc);
227 mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");
228 mobility.Install(c);
229
231 internet.Install(c);
232
233 TypeId tid = TypeId::LookupByName("ns3::UdpSocketFactory");
234 Ptr<Socket> recvSink = Socket::CreateSocket(c.Get(0), tid);
235 InetSocketAddress local = InetSocketAddress(Ipv4Address("10.1.1.1"), 80);
236 recvSink->Bind(local);
237 recvSink->SetRecvCallback(MakeCallback(&ReceivePacket));
238
239 Ptr<Socket> source = Socket::CreateSocket(c.Get(1), tid);
240 InetSocketAddress remote = InetSocketAddress(Ipv4Address("255.255.255.255"), 80);
241 source->SetAllowBroadcast(true);
242 source->Connect(remote);
243
244 // Interferer will send to a different port; we will not see a
245 // "Received packet" message
246 Ptr<Socket> interferer = Socket::CreateSocket(c.Get(2), tid);
247 InetSocketAddress interferingAddr = InetSocketAddress(Ipv4Address("255.255.255.255"), 49000);
248 interferer->SetAllowBroadcast(true);
249 interferer->Connect(interferingAddr);
250
251 // Tracing
252 wifiPhy.EnablePcap("wifi-simple-interference", devices.Get(0));
253
254 // Output what we are doing
255 NS_LOG_UNCOND("Primary packet RSS=" << Prss << " dBm and interferer RSS=" << Irss
256 << " dBm at time offset=" << delta.As(Time::US));
257
258 Simulator::ScheduleWithContext(source->GetNode()->GetId(),
259 startTime,
261 source,
262 PpacketSize,
263 numPackets,
264 interPacketInterval);
265
266 Simulator::ScheduleWithContext(interferer->GetNode()->GetId(),
267 startTime + delta,
269 interferer,
270 IpacketSize,
271 numPackets,
272 interPacketInterval);
273
276
277 return 0;
278}
a polymophic address class
Definition address.h:90
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.
Ipv4 addresses are stored in host order in this class.
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.
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition simulator.h:560
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:131
static void ScheduleWithContext(uint32_t context, const Time &delay, FUNC f, Ts &&... args)
Schedule an event with the given context.
Definition simulator.h:577
static void Run()
Run the simulation.
Definition simulator.cc:167
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:94
@ US
microsecond
Definition nstime.h:107
a unique identifier for an interface.
Definition type-id.h:48
static TypeId LookupByName(std::string name)
Get a TypeId by name.
Definition type-id.cc:872
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)
@ 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 SetDefault(std::string name, const AttributeValue &value)
Definition config.cc:883
#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:191
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:619
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition ptr.h:436
@ WIFI_STANDARD_80211b
devices
Definition first.py:31
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:684
wifi
Definition third.py:84
mobility
Definition third.py:92
bool verbose
uint32_t pktSize
packet size used for the simulation (in bytes)
static 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.
static std::string PrintReceivedPacket(Ptr< Socket > socket)
Print a packer that has been received.