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 * 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 three nodes on an 802.11b physical layer, with
20// 802.11b NICs in adhoc mode. There is a transmitter, receiver, and
21// interferer. The transmitter sends one packet to the receiver and
22// the receiver receives it with a certain configurable RSS (by default,
23// -80 dBm). The interferer does not do carrier sense and also sends
24// the packet to interfere with the primary packet. The channel model
25// is clear channel.
26//
27// Therefore, at the receiver, the reception looks like this:
28//
29// ------------------time---------------->
30// t0
31//
32// |------------------------------------|
33// | |
34// | primary received frame (time t0) |
35// | |
36// |------------------------------------|
37//
38//
39// t1
40// |-----------------------------------|
41// | |
42// | interfering frame (time t1) |
43// | |
44// |-----------------------------------|
45//
46// The orientation is:
47// n2 ---------> n0 <---------- n1
48// interferer receiver transmitter
49//
50// The configurable parameters are:
51// - Prss (primary rss) (-80 dBm default)
52// - Irss (interfering rss) (-95 dBm default)
53// - delta (microseconds, (t1-t0), may be negative, default 0)
54// - PpacketSize (primary packet size) (bytes, default 1000)
55// - IpacketSize (interferer packet size) (bytes, default 1000)
56//
57// For instance, for this configuration, the interfering frame arrives
58// at -90 dBm with a time offset of 3.2 microseconds:
59//
60// ./ns3 run "wifi-simple-interference --Irss=-90 --delta=3.2"
61//
62// Note that all ns-3 attributes (not just the ones exposed in the below
63// script) can be changed at command line; see the documentation.
64//
65// This script can also be helpful to put the Wifi layer into verbose
66// logging mode; this command will turn on all wifi logging:
67//
68// ./ns3 run "wifi-simple-interference --verbose=1"
69//
70// When you are done, you will notice a pcap trace file in your directory.
71// If you have tcpdump installed, you can try this:
72//
73// tcpdump -r wifi-simple-interference-0-0.pcap -nn -tt
74// reading from file wifi-simple-interference-0-0.pcap, link-type IEEE802_11_RADIO (802.11 plus BSD
75// radio information header) 10.008704 10008704us tsft 1.0 Mb/s 2437 MHz (0x00c0) -80dB signal -98dB
76// noise IP 10.1.1.2.49153 > 10.1.1.255.80: UDP, length 1000
77//
78// Next, try this command and look at the tcpdump-- you should see two packets
79// that are no longer interfering:
80// ./ns3 run "wifi-simple-interference --delta=30000"
81
82#include "ns3/command-line.h"
83#include "ns3/config.h"
84#include "ns3/double.h"
85#include "ns3/internet-stack-helper.h"
86#include "ns3/log.h"
87#include "ns3/mobility-helper.h"
88#include "ns3/mobility-model.h"
89#include "ns3/ssid.h"
90#include "ns3/string.h"
91#include "ns3/yans-wifi-channel.h"
92#include "ns3/yans-wifi-helper.h"
93
94using namespace ns3;
95
96NS_LOG_COMPONENT_DEFINE("WifiSimpleInterference");
97
98/**
99 * Print a packer that has been received.
100 *
101 * \param socket The receiving socket.
102 * \return a string with the packet details.
103 */
104static inline std::string
106{
107 Address addr;
108
109 std::ostringstream oss;
110
111 while (socket->Recv())
112 {
113 socket->GetSockName(addr);
115
116 oss << "Received one packet! Socket: " << iaddr.GetIpv4() << " port: " << iaddr.GetPort();
117 }
118
119 return oss.str();
120}
121
122/**
123 * Function called when a packet is received.
124 *
125 * \param socket The receiving socket.
126 */
127static void
129{
131}
132
133/**
134 * Generate traffic
135 *
136 * \param socket The seding socket.
137 * \param pktSize The packet size.
138 * \param pktCount The packet counter.
139 * \param pktInterval The interval between two packets.
140 */
141static void
143{
144 if (pktCount > 0)
145 {
146 socket->Send(Create<Packet>(pktSize));
147 Simulator::Schedule(pktInterval,
149 socket,
150 pktSize,
151 pktCount - 1,
152 pktInterval);
153 }
154 else
155 {
156 socket->Close();
157 }
158}
159
160int
161main(int argc, char* argv[])
162{
163 std::string phyMode("DsssRate1Mbps");
164 double Prss = -80; // -dBm
165 double Irss = -95; // -dBm
166 double delta = 0; // microseconds
167 uint32_t PpacketSize = 1000; // bytes
168 uint32_t IpacketSize = 1000; // bytes
169 bool verbose = false;
170
171 // these are not command line arguments for this version
172 uint32_t numPackets = 1;
173 double interval = 1.0; // seconds
174 double startTime = 10.0; // seconds
175 double distanceToRx = 100.0; // meters
176
177 double offset = 91; // This is a magic number used to set the
178 // transmit power, based on other configuration
179 CommandLine cmd(__FILE__);
180 cmd.AddValue("phyMode", "Wifi Phy mode", phyMode);
181 cmd.AddValue("Prss", "Intended primary received signal strength (dBm)", Prss);
182 cmd.AddValue("Irss", "Intended interfering received signal strength (dBm)", Irss);
183 cmd.AddValue("delta", "time offset (microseconds) for interfering signal", delta);
184 cmd.AddValue("PpacketSize", "size of application packet sent", PpacketSize);
185 cmd.AddValue("IpacketSize", "size of interfering packet sent", IpacketSize);
186 cmd.AddValue("verbose", "turn on all WifiNetDevice log components", verbose);
187 cmd.Parse(argc, argv);
188 // Convert to time object
189 Time interPacketInterval = Seconds(interval);
190
191 // Fix non-unicast data rate to be the same as that of unicast
192 Config::SetDefault("ns3::WifiRemoteStationManager::NonUnicastMode", StringValue(phyMode));
193
195 c.Create(3);
196
197 // The below set of helpers will help us to put together the wifi NICs we want
199 if (verbose)
200 {
201 WifiHelper::EnableLogComponents(); // Turn on all Wifi logging
202 }
203 wifi.SetStandard(WIFI_STANDARD_80211b);
204
205 YansWifiPhyHelper wifiPhy;
206
207 // ns-3 supports RadioTap and Prism tracing extensions for 802.11b
209
210 YansWifiChannelHelper wifiChannel;
211 wifiChannel.SetPropagationDelay("ns3::ConstantSpeedPropagationDelayModel");
212 wifiChannel.AddPropagationLoss("ns3::LogDistancePropagationLossModel");
213 wifiPhy.SetChannel(wifiChannel.Create());
214
215 // Add a mac and disable rate control
216 WifiMacHelper wifiMac;
217 wifi.SetRemoteStationManager("ns3::ConstantRateWifiManager",
218 "DataMode",
219 StringValue(phyMode),
220 "ControlMode",
221 StringValue(phyMode));
222 // Set it to adhoc mode
223 wifiMac.SetType("ns3::AdhocWifiMac");
224 NetDeviceContainer devices = wifi.Install(wifiPhy, wifiMac, c.Get(0));
225 // This will disable these sending devices from detecting a signal
226 // so that they do not backoff
227 wifiPhy.Set("TxGain", DoubleValue(offset + Prss));
228 devices.Add(wifi.Install(wifiPhy, wifiMac, c.Get(1)));
229 wifiPhy.Set("TxGain", DoubleValue(offset + Irss));
230 devices.Add(wifi.Install(wifiPhy, wifiMac, c.Get(2)));
231
232 // Note that with FixedRssLossModel, the positions below are not
233 // used for received signal strength.
235 Ptr<ListPositionAllocator> positionAlloc = CreateObject<ListPositionAllocator>();
236 positionAlloc->Add(Vector(0.0, 0.0, 0.0));
237 positionAlloc->Add(Vector(distanceToRx, 0.0, 0.0));
238 positionAlloc->Add(Vector(-1 * distanceToRx, 0.0, 0.0));
239 mobility.SetPositionAllocator(positionAlloc);
240 mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");
241 mobility.Install(c);
242
244 internet.Install(c);
245
246 TypeId tid = TypeId::LookupByName("ns3::UdpSocketFactory");
247 Ptr<Socket> recvSink = Socket::CreateSocket(c.Get(0), tid);
248 InetSocketAddress local = InetSocketAddress(Ipv4Address("10.1.1.1"), 80);
249 recvSink->Bind(local);
250 recvSink->SetRecvCallback(MakeCallback(&ReceivePacket));
251
252 Ptr<Socket> source = Socket::CreateSocket(c.Get(1), tid);
253 InetSocketAddress remote = InetSocketAddress(Ipv4Address("255.255.255.255"), 80);
254 source->SetAllowBroadcast(true);
255 source->Connect(remote);
256
257 // Interferer will send to a different port; we will not see a
258 // "Received packet" message
259 Ptr<Socket> interferer = Socket::CreateSocket(c.Get(2), tid);
260 InetSocketAddress interferingAddr = InetSocketAddress(Ipv4Address("255.255.255.255"), 49000);
261 interferer->SetAllowBroadcast(true);
262 interferer->Connect(interferingAddr);
263
264 // Tracing
265 wifiPhy.EnablePcap("wifi-simple-interference", devices.Get(0));
266
267 // Output what we are doing
268 NS_LOG_UNCOND("Primary packet RSS=" << Prss << " dBm and interferer RSS=" << Irss
269 << " dBm at time offset=" << delta << " ms");
270
271 Simulator::ScheduleWithContext(source->GetNode()->GetId(),
272 Seconds(startTime),
274 source,
275 PpacketSize,
276 numPackets,
277 interPacketInterval);
278
279 Simulator::ScheduleWithContext(interferer->GetNode()->GetId(),
280 Seconds(startTime + delta / 1000000.0),
282 interferer,
283 IpacketSize,
284 numPackets,
285 interPacketInterval);
286
289
290 return 0;
291}
a polymophic address class
Definition: address.h:101
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
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.
Definition: ipv4-address.h:42
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:77
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 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
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:836
helps to create WifiNetDevice objects
Definition: wifi-helper.h:324
static void EnableLogComponents(LogLevel logLevel=LOG_LEVEL_ALL)
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:894
#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:202
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1319
@ WIFI_STANDARD_80211b
ns 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:706
ns cmd
Definition: second.py:40
ns wifi
Definition: third.py:95
ns mobility
Definition: third.py:105
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.