A Discrete-Event Network Simulator
API
wifi-interference-test-suite.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2009 University of Washington
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 
19 #include "ns3/log.h"
20 #include "ns3/test.h"
21 #include "ns3/config.h"
22 #include "ns3/string.h"
23 #include "ns3/double.h"
24 #include "ns3/boolean.h"
25 #include "ns3/internet-stack-helper.h"
26 #include "ns3/ipv4-address-helper.h"
27 #include "ns3/yans-wifi-helper.h"
28 #include "ns3/yans-wifi-channel.h"
29 #include "ns3/mobility-helper.h"
30 
31 using namespace ns3;
32 
33 NS_LOG_COMPONENT_DEFINE ("WifiInterferenceTestSuite");
34 
36 {
37 public:
39  virtual ~WifiInterferenceTestCase ();
40 
41 private:
42  virtual void DoRun (void);
43  void ReceivePacket (Ptr<Socket> socket);
44  static void GenerateTraffic (Ptr<Socket> socket, uint32_t pktSize, uint32_t pktCount, Time pktInterval);
45  void PrintEndSync (std::string context, uint32_t dataRate, double snr, double per);
46  double WifiSimpleInterference (std::string phyMode, double Prss, double Irss, double delta, uint32_t PpacketSize,
47  uint32_t IpacketSize, bool verbose, InternetStackHelper internet, WifiPhyStandard wifiStandard);
48  double m_PER;
49  double m_SNR;
50  uint32_t m_DataRate;
51 };
52 
53 // Add some help text to this case to describe what it is intended to test
55  : TestCase ("Test interference calculation when interfering frame exactly overlaps intended frame")
56 {
57 }
58 
60 {
61 }
62 
63 static inline std::string
65 {
66  Address addr;
67  socket->GetSockName (addr);
68  InetSocketAddress iaddr = InetSocketAddress::ConvertFrom (addr);
69 
70  std::ostringstream oss;
71  oss << "Received one packet! Socket: " << iaddr.GetIpv4 () << " port: " << iaddr.GetPort ();
72 
73  return oss.str ();
74 }
75 
76 void
78 {
80 }
81 
82 void
83 WifiInterferenceTestCase::GenerateTraffic (Ptr<Socket> socket, uint32_t pktSize, uint32_t pktCount, Time pktInterval)
84 {
85  if (pktCount > 0)
86  {
87  socket->Send (Create<Packet> (pktSize));
88  Simulator::Schedule (pktInterval, &WifiInterferenceTestCase::GenerateTraffic, socket, pktSize, pktCount-1, pktInterval);
89  }
90  else
91  {
92  socket->Close ();
93  }
94 }
95 
96 void
97 WifiInterferenceTestCase::PrintEndSync (std::string context, uint32_t dataRate, double snr, double per)
98 {
99  NS_LOG_UNCOND ("EndSync: Received frame with dataRate=" << dataRate << ", SNR=" << snr << ", PER =" << per);
100  m_PER = per;
101  m_SNR = snr;
102  m_DataRate = dataRate;
103 }
104 
105 double
106 WifiInterferenceTestCase::WifiSimpleInterference (std::string phyMode,double Prss, double Irss, double delta, uint32_t PpacketSize, uint32_t IpacketSize, bool verbose, InternetStackHelper internet, WifiPhyStandard wifiStandard)
107 {
108 
109  uint32_t numPackets = 1;
110  double interval = 1.0; // seconds
111  double startTime = 10.0; // seconds
112  double distanceToRx = 100.0; // meters
113 
114  double offset = 91; // This is a magic number used to set the
115  // transmit power, based on other configuration
116 
117  m_PER = 0;
118  m_SNR = 0;
119  m_DataRate = 0;
120 
121  // Convert to time object
122  Time interPacketInterval = Seconds (interval);
123 
124  // disable fragmentation for frames below 2200 bytes
125  Config::SetDefault ("ns3::WifiRemoteStationManager::FragmentationThreshold", StringValue ("2200"));
126  // turn off RTS/CTS for frames below 2200 bytes
127  Config::SetDefault ("ns3::WifiRemoteStationManager::RtsCtsThreshold", StringValue ("2200"));
128  // Fix non-unicast data rate to be the same as that of unicast
129  Config::SetDefault ("ns3::WifiRemoteStationManager::NonUnicastMode",
130  StringValue (phyMode));
131 
132  NodeContainer c;
133  c.Create (3);
134 
135  // The below set of helpers will help us to put together the wifi NICs we want
137  wifi.SetStandard (wifiStandard);
138 
139  YansWifiPhyHelper wifiPhy = YansWifiPhyHelper::Default ();
140  // This is one parameter that matters when using FixedRssLossModel
141  // set it to zero; otherwise, gain will be added
142  wifiPhy.Set ("RxGain", DoubleValue (0) );
143  wifiPhy.Set ("CcaMode1Threshold", DoubleValue (0.0) );
144 
145  // ns-3 supports RadioTap and Prism tracing extensions for 802.11b
146  wifiPhy.SetPcapDataLinkType (WifiPhyHelper::DLT_IEEE802_11_RADIO);
147 
148  YansWifiChannelHelper wifiChannel;
149  wifiChannel.SetPropagationDelay ("ns3::ConstantSpeedPropagationDelayModel");
150  wifiChannel.AddPropagationLoss ("ns3::LogDistancePropagationLossModel");
151  wifiPhy.SetChannel (wifiChannel.Create ());
152 
153  // Add a mac and disable rate control
154  WifiMacHelper wifiMac;
155  wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager",
156  "DataMode",StringValue (phyMode),
157  "ControlMode",StringValue (phyMode));
158 
159  // Set it to adhoc mode (and enable QOS for HT/VHT stations)
160  if (wifiStandard == WIFI_PHY_STANDARD_80211n_2_4GHZ
161  || wifiStandard == WIFI_PHY_STANDARD_80211n_5GHZ
162  || wifiStandard == WIFI_PHY_STANDARD_80211ac)
163  {
164  wifiMac.SetType ("ns3::AdhocWifiMac",
165  "QosSupported", BooleanValue (true));
166  }
167  else
168  {
169  wifiMac.SetType ("ns3::AdhocWifiMac");
170  }
171  NetDeviceContainer devices = wifi.Install (wifiPhy, wifiMac, c.Get (0));
172 
173  // This will disable these sending devices from detecting a signal
174  // so that they do not backoff
175  wifiPhy.Set ("EnergyDetectionThreshold", DoubleValue (0.0) );
176  wifiPhy.Set ("TxGain", DoubleValue (offset + Prss) );
177  devices.Add (wifi.Install (wifiPhy, wifiMac, c.Get (1)));
178  wifiPhy.Set ("TxGain", DoubleValue (offset + Irss) );
179  devices.Add (wifi.Install (wifiPhy, wifiMac, c.Get (2)));
180 
181  // Note that with FixedRssLossModel, the positions below are not
182  // used for received signal strength.
184  Ptr<ListPositionAllocator> positionAlloc = CreateObject<ListPositionAllocator> ();
185  positionAlloc->Add (Vector (0.0, 0.0, 0.0));
186  positionAlloc->Add (Vector (distanceToRx, 0.0, 0.0));
187  positionAlloc->Add (Vector (-1*distanceToRx, 0.0, 0.0));
188  mobility.SetPositionAllocator (positionAlloc);
189  mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
190  mobility.Install (c);
191 
192  // InternetStackHelper internet;
193  internet.Install (c);
194 
195  Ipv4AddressHelper ipv4;
196  NS_LOG_INFO ("Assign IP Addresses.");
197  ipv4.SetBase ("10.1.1.0", "255.255.255.0");
199 
200  TypeId tid = TypeId::LookupByName ("ns3::UdpSocketFactory");
201  Ptr<Socket> recvSink = Socket::CreateSocket (c.Get (0), tid);
202  InetSocketAddress local = InetSocketAddress (Ipv4Address ("10.1.1.1"), 80);
203  recvSink->Bind (local);
205 
206  Ptr<Socket> source = Socket::CreateSocket (c.Get (1), tid);
207  InetSocketAddress remote = InetSocketAddress (Ipv4Address ("255.255.255.255"), 80);
208  source->Connect (remote);
209 
210  // Interferer will send to a different port; we will not see a
211  // "Received packet" message
212  Ptr<Socket> interferer = Socket::CreateSocket (c.Get (2), tid);
213  InetSocketAddress interferingAddr = InetSocketAddress (Ipv4Address ("255.255.255.255"), 49000);
214  interferer->Connect (interferingAddr);
215 
216  Config::Connect ("/NodeList/0/DeviceList/0/$ns3::WifiNetDevice/Phy/$ns3::YansWifiPhy/EndSync", MakeCallback (&WifiInterferenceTestCase::PrintEndSync, this));
217  // Tracing
218 // wifiPhy.EnablePcap ("wifi-simple-interference", devices.Get (0));
219 
220  Simulator::ScheduleWithContext (source->GetNode ()->GetId (),
222  source, PpacketSize, numPackets, interPacketInterval);
223 
224  Simulator::ScheduleWithContext (interferer->GetNode ()->GetId (),
225  Seconds (startTime + delta/1000000.0), &GenerateTraffic,
226  interferer, IpacketSize, numPackets, interPacketInterval);
227 
228  Simulator::Run ();
229  Simulator::Destroy ();
230 
231  return m_PER;
232 }
233 
234 void
236 {
237 
238  std::string phyMode ("DsssRate1Mbps");
240  double Prss = -90; // -dBm
241  double Irss = -90; // -dBm
242  double delta = 0; // microseconds
243  uint32_t PpacketSize = 1000; // bytes
244  uint32_t IpacketSize = 1000; // bytes
245  bool verbose = false;
246  double PER, PER1, PER2;
247  InternetStackHelper internet;
248 
249  // Compute the packet error rate (PER) when delta=0 microseconds. This
250  // means that the interferer arrives at exactly the same time as the
251  // intended packet
252  PER = WifiSimpleInterference (phyMode,Prss,Irss,delta,PpacketSize,IpacketSize,verbose,internet,wifiStandard);
253 
254  // Now rerun this test case and compute the PER when the delta time between
255  // arrival of the intended frame and interferer is 1 microsecond.
256  delta = 1;
257  PER1 = WifiSimpleInterference (phyMode,Prss,Irss,delta,PpacketSize,IpacketSize,verbose,internet,wifiStandard);
258 
259  // Now rerun this test case and compute the PER when the delta time between
260  // arrival of the intended frame and interferer is 2 microseconds.
261  delta = 2;
262  PER2 = WifiSimpleInterference (phyMode,Prss,Irss,delta,PpacketSize,IpacketSize,verbose,internet,wifiStandard);
263 
264  double PERDiff1 = PER - PER1;
265 
266  double PERDiff2 = PER1 - PER2;
267 
268  NS_TEST_ASSERT_MSG_EQ (PERDiff1, PERDiff2,
269  "The PER difference due to 1 microsecond difference in arrival shouldn't depend on absolute arrival");
270  //Now rerun for 11n
271  wifiStandard = WIFI_PHY_STANDARD_80211n_2_4GHZ;
272  // Compute the packet error rate (PER) when delta=0 microseconds. This
273  // means that the interferer arrives at exactly the same time as the
274  // intended packet
275  PER = WifiSimpleInterference (phyMode,Prss,Irss,delta,PpacketSize,IpacketSize,verbose,internet,wifiStandard);
276 
277  // Now rerun this test case and compute the PER when the delta time between
278  // arrival of the intended frame and interferer is 1 microsecond.
279  delta = 1;
280  PER1 = WifiSimpleInterference (phyMode,Prss,Irss,delta,PpacketSize,IpacketSize,verbose,internet,wifiStandard);
281 
282  // Now rerun this test case and compute the PER when the delta time between
283  // arrival of the intended frame and interferer is 2 microseconds.
284  delta = 2;
285  PER2 = WifiSimpleInterference (phyMode,Prss,Irss,delta,PpacketSize,IpacketSize,verbose,internet,wifiStandard);
286 
287  PERDiff1 = PER - PER1;
288 
289  PERDiff2 = PER1 - PER2;
290 
291  NS_TEST_ASSERT_MSG_EQ (PERDiff1, PERDiff2,
292  "The PER difference due to 1 microsecond difference in arrival shouldn't depend on absolute arrival");
293 
294 }
295 
297 {
298 public:
300 };
301 
303  : TestSuite ("ns3-wifi-interference", UNIT)
304 {
305  AddTestCase (new WifiInterferenceTestCase, TestCase::QUICK);
306 }
307 
309 
void AddPropagationLoss(std::string name, 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())
void Set(std::string name, const AttributeValue &v)
Definition: wifi-helper.cc:134
virtual void DoRun(void)
Implementation to actually run this TestCase.
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:102
an Inet address class
AttributeValue implementation for Boolean.
Definition: boolean.h:36
HT PHY for the 5 GHz band (clause 20)
uint32_t GetId(void) const
Definition: node.cc:107
holds a vector of std::pair of Ptr<Ipv4> and interface index.
static void GenerateTraffic(Ptr< Socket > socket, uint32_t pktSize, uint32_t pktCount, Time pktInterval)
Hold variables of type string.
Definition: string.h:41
Make it easy to create and manage PHY objects for the yans model.
double WifiSimpleInterference(std::string phyMode, double Prss, double Irss, double delta, uint32_t PpacketSize, uint32_t IpacketSize, bool verbose, InternetStackHelper internet, WifiPhyStandard wifiStandard)
A suite of tests to run.
Definition: test.h:1342
static void GenerateTraffic(Ptr< Socket > socket, uint32_t pktSize, Ptr< Node > n, uint32_t pktCount, Time pktInterval)
void ReceivePacket(Ptr< Socket > socket)
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
virtual int GetSockName(Address &address) const =0
Get socket address.
aggregate IP/TCP/UDP functionality to existing Nodes.
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:278
HT PHY for the 2.4 GHz band (clause 20)
encapsulates test code
Definition: test.h:1155
helps to create WifiNetDevice objects
Definition: wifi-helper.h:230
a polymophic address class
Definition: address.h:90
Ptr< YansWifiChannel > Create(void) const
mobility
Definition: third.py:101
void SetChannel(Ptr< YansWifiChannel > channel)
WifiPhyStandard
Identifies the PHY specification that a Wifi device is configured to use.
static std::string PrintReceivedPacket(Ptr< Socket > socket)
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:299
static WifiInterferenceTestSuite wifiInterferenceTestSuite
double startTime
#define NS_TEST_ASSERT_MSG_EQ(actual, limit, msg)
Test that an actual and expected (limit) value are equal and report and abort if not.
Definition: test.h:168
holds a vector of ns3::NetDevice pointers
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1489
void SetRecvCallback(Callback< void, Ptr< Socket > >)
Notify application when new data is available to be read.
Definition: socket.cc:128
void Connect(std::string path, const CallbackBase &cb)
Definition: config.cc:843
virtual int Connect(const Address &address)=0
Initiate a connection to a remote host.
virtual int Bind(const Address &address)=0
Allocate a local endpoint for this socket.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
keep track of a set of node pointers.
DSSS PHY (Clause 15) and HR/DSSS PHY (Clause 18)
uint16_t GetPort(void) const
#define NS_LOG_UNCOND(msg)
Output the requested message unconditionally.
manage and create wifi channel objects for the yans model.
create MAC layers for a ns3::WifiNetDevice.
void SetPcapDataLinkType(SupportedPcapDataLinkTypes dlt)
Set the data link type of PCAP traces to be used.
Definition: wifi-helper.cc:453
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(), std::string n8="", const AttributeValue &v8=EmptyAttributeValue(), std::string n9="", const AttributeValue &v9=EmptyAttributeValue(), std::string n10="", const AttributeValue &v10=EmptyAttributeValue())
void Install(std::string nodeName) const
Aggregate implementations of the ns3::Ipv4, ns3::Ipv6, ns3::Udp, and ns3::Tcp classes onto the provid...
wifi
Definition: third.py:89
Helper class used to assign positions and mobility models to nodes.
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:40
Ipv4InterfaceContainer Assign(const NetDeviceContainer &c)
Assign IP addresses to the net devices specified in the container based on the current network prefix...
virtual Ptr< Node > GetNode(void) const =0
Return the node this socket is associated with.
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1014
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:782
void Add(Vector v)
Add a position to the list of positions.
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
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.
void SetPropagationDelay(std::string name, 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())
devices
Definition: first.py:32
virtual int Send(Ptr< Packet > p, uint32_t flags)=0
Send data (or dummy data) to the remote host.
virtual int Close(void)=0
Close a socket.
void ReceivePacket(Ptr< Socket > socket)
This class can be used to hold variables of floating point type such as &#39;double&#39; or &#39;float&#39;...
Definition: double.h:41
a unique identifier for an interface.
Definition: type-id.h:58
void PrintEndSync(std::string context, uint32_t dataRate, double snr, double per)
Ipv4Address GetIpv4(void) const
void SetBase(Ipv4Address network, Ipv4Mask mask, Ipv4Address base="0.0.0.1")
Set the base network number, network mask and base address.
bool verbose