1 /* -*-  Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2009 MIRKO BANCHI
4  * Copyright (c) 2015 University of Washington
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation;
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  *
19  * Authors: Mirko Banchi <mk.banchi@gmail.com>
20  *          Sebastien Deronne <sebastien.deronne@gmail.com>
21  *          Tom Henderson <tomhend@u.washington.edu>
22  *
23  * Adapted from wifi-ht-network.cc example
24  */
25 
26 #include <iomanip>
27 #include "ns3/command-line.h"
28 #include "ns3/config.h"
29 #include "ns3/uinteger.h"
30 #include "ns3/boolean.h"
31 #include "ns3/double.h"
32 #include "ns3/string.h"
33 #include "ns3/log.h"
34 #include "ns3/yans-wifi-helper.h"
35 #include "ns3/spectrum-wifi-helper.h"
36 #include "ns3/ssid.h"
37 #include "ns3/mobility-helper.h"
38 #include "ns3/internet-stack-helper.h"
39 #include "ns3/ipv4-address-helper.h"
40 #include "ns3/udp-client-server-helper.h"
41 #include "ns3/packet-sink-helper.h"
42 #include "ns3/on-off-helper.h"
43 #include "ns3/ipv4-global-routing-helper.h"
44 #include "ns3/packet-sink.h"
45 #include "ns3/yans-wifi-channel.h"
46 #include "ns3/multi-model-spectrum-channel.h"
47 #include "ns3/propagation-loss-model.h"
48 
49 // This is a simple example of an IEEE 802.11n Wi-Fi network.
50 //
51 // The main use case is to enable and test SpectrumWifiPhy vs YansWifiPhy
52 // for packet error ratio
53 //
54 // Network topology:
55 //
56 //  Wi-Fi 192.168.1.0
57 //
58 //   STA                  AP
59 //    * <-- distance -->  *
60 //    |                   |
61 //    n1                  n2
62 //
63 // Users may vary the following command-line arguments in addition to the
64 // attributes, global values, and default values typically available:
65 //
66 //    --simulationTime:  Simulation time in seconds [10]
67 //    --udp:             UDP if set to 1, TCP otherwise [true]
68 //    --distance:        meters separation between nodes [50]
69 //    --index:           restrict index to single value between 0 and 31 [256]
70 //    --wifiType:        select ns3::SpectrumWifiPhy or ns3::YansWifiPhy [ns3::SpectrumWifiPhy]
71 //    --errorModelType:  select ns3::NistErrorRateModel or ns3::YansErrorRateModel [ns3::NistErrorRateModel]
72 //    --enablePcap:      enable pcap output [false]
73 //
74 // By default, the program will step through 32 index values, corresponding
75 // to the following MCS, channel width, and guard interval combinations:
76 //   index 0-7:    MCS 0-7, long guard interval, 20 MHz channel
77 //   index 8-15:   MCS 0-7, short guard interval, 20 MHz channel
78 //   index 16-23:  MCS 0-7, long guard interval, 40 MHz channel
79 //   index 24-31:  MCS 0-7, short guard interval, 40 MHz channel
80 // and send UDP for 10 seconds using each MCS, using the SpectrumWifiPhy and the
81 // NistErrorRateModel, at a distance of 50 meters.  The program outputs
82 // results such as:
83 //
84 // wifiType: ns3::SpectrumWifiPhy distance: 50m; time: 10; TxPower: 1 dBm (1.3 mW)
85 // index   MCS  Rate (Mb/s) Tput (Mb/s) Received Signal (dBm) Noise (dBm) SNR (dB)
86 //     0     0      6.50        5.77    7414      -79.71      -93.97       14.25
87 //     1     1     13.00       11.58   14892      -79.71      -93.97       14.25
88 //     2     2     19.50       17.39   22358      -79.71      -93.97       14.25
89 //     3     3     26.00       22.96   29521      -79.71      -93.97       14.25
90 //   ...
91 //
92 
93 using namespace ns3;
94 
95 // Global variables for use in callbacks.
96 double g_signalDbmAvg;
97 double g_noiseDbmAvg;
98 uint32_t g_samples;
99 
MonitorSniffRx(Ptr<const Packet> packet,uint16_t channelFreqMhz,WifiTxVector txVector,MpduInfo aMpdu,SignalNoiseDbm signalNoise,uint16_t staId)100 void MonitorSniffRx (Ptr<const Packet> packet,
101                      uint16_t channelFreqMhz,
102                      WifiTxVector txVector,
103                      MpduInfo aMpdu,
104                      SignalNoiseDbm signalNoise,
105                      uint16_t staId)
106 
107 {
108   g_samples++;
109   g_signalDbmAvg += ((signalNoise.signal - g_signalDbmAvg) / g_samples);
110   g_noiseDbmAvg += ((signalNoise.noise - g_noiseDbmAvg) / g_samples);
111 }
112 
113 NS_LOG_COMPONENT_DEFINE ("WifiSpectrumPerExample");
114 
main(int argc,char * argv[])115 int main (int argc, char *argv[])
116 {
117   bool udp = true;
118   double distance = 50;
119   double simulationTime = 10; //seconds
120   uint16_t index = 256;
121   std::string wifiType = "ns3::SpectrumWifiPhy";
122   std::string errorModelType = "ns3::NistErrorRateModel";
123   bool enablePcap = false;
124   const uint32_t tcpPacketSize = 1448;
125 
126   CommandLine cmd (__FILE__);
127   cmd.AddValue ("simulationTime", "Simulation time in seconds", simulationTime);
128   cmd.AddValue ("udp", "UDP if set to 1, TCP otherwise", udp);
129   cmd.AddValue ("distance", "meters separation between nodes", distance);
130   cmd.AddValue ("index", "restrict index to single value between 0 and 31", index);
131   cmd.AddValue ("wifiType", "select ns3::SpectrumWifiPhy or ns3::YansWifiPhy", wifiType);
132   cmd.AddValue ("errorModelType", "select ns3::NistErrorRateModel or ns3::YansErrorRateModel", errorModelType);
133   cmd.AddValue ("enablePcap", "enable pcap output", enablePcap);
134   cmd.Parse (argc,argv);
135 
136   uint16_t startIndex = 0;
137   uint16_t stopIndex = 31;
138   if (index < 32)
139     {
140       startIndex = index;
141       stopIndex = index;
142     }
143 
144   std::cout << "wifiType: " << wifiType << " distance: " << distance << "m; time: " << simulationTime << "; TxPower: 1 dBm (1.3 mW)" << std::endl;
145   std::cout << std::setw (5) << "index" <<
146     std::setw (6) << "MCS" <<
147     std::setw (13) << "Rate (Mb/s)" <<
148     std::setw (12) << "Tput (Mb/s)" <<
149     std::setw (10) << "Received " <<
150     std::setw (12) << "Signal (dBm)" <<
151     std::setw (12) << "Noise (dBm)" <<
152     std::setw (9) << "SNR (dB)" <<
153     std::endl;
154   for (uint16_t i = startIndex; i <= stopIndex; i++)
155     {
156       uint32_t payloadSize;
157       if (udp)
158         {
159           payloadSize = 972; // 1000 bytes IPv4
160         }
161       else
162         {
163           payloadSize = 1448; // 1500 bytes IPv6
164           Config::SetDefault ("ns3::TcpSocket::SegmentSize", UintegerValue (payloadSize));
165         }
166 
167       NodeContainer wifiStaNode;
168       wifiStaNode.Create (1);
169       NodeContainer wifiApNode;
170       wifiApNode.Create (1);
171 
172       YansWifiPhyHelper phy;
173       SpectrumWifiPhyHelper spectrumPhy;
174       if (wifiType == "ns3::YansWifiPhy")
175         {
176           YansWifiChannelHelper channel;
177           channel.AddPropagationLoss ("ns3::FriisPropagationLossModel",
178                                       "Frequency", DoubleValue (5.180e9));
179           channel.SetPropagationDelay ("ns3::ConstantSpeedPropagationDelayModel");
180           phy.SetChannel (channel.Create ());
181           phy.Set ("TxPowerStart", DoubleValue (1)); // dBm (1.26 mW)
182           phy.Set ("TxPowerEnd", DoubleValue (1));
183         }
184       else if (wifiType == "ns3::SpectrumWifiPhy")
185         {
186           Ptr<MultiModelSpectrumChannel> spectrumChannel
187             = CreateObject<MultiModelSpectrumChannel> ();
188           Ptr<FriisPropagationLossModel> lossModel
189             = CreateObject<FriisPropagationLossModel> ();
190           lossModel->SetFrequency (5.180e9);
191           spectrumChannel->AddPropagationLossModel (lossModel);
192 
193           Ptr<ConstantSpeedPropagationDelayModel> delayModel
194             = CreateObject<ConstantSpeedPropagationDelayModel> ();
195           spectrumChannel->SetPropagationDelayModel (delayModel);
196 
197           spectrumPhy.SetChannel (spectrumChannel);
198           spectrumPhy.SetErrorRateModel (errorModelType);
199           spectrumPhy.Set ("TxPowerStart", DoubleValue (1)); // dBm  (1.26 mW)
200           spectrumPhy.Set ("TxPowerEnd", DoubleValue (1));
201         }
202       else
203         {
204           NS_FATAL_ERROR ("Unsupported WiFi type " << wifiType);
205         }
206 
207 
208       WifiHelper wifi;
209       wifi.SetStandard (WIFI_STANDARD_80211n_5GHZ);
210       WifiMacHelper mac;
211 
212       Ssid ssid = Ssid ("ns380211n");
213 
214       double datarate = 0;
215       StringValue DataRate;
216       if (i == 0)
217         {
218           DataRate = StringValue ("HtMcs0");
219           datarate = 6.5;
220         }
221       else if (i == 1)
222         {
223           DataRate = StringValue ("HtMcs1");
224           datarate = 13;
225         }
226       else if (i == 2)
227         {
228           DataRate = StringValue ("HtMcs2");
229           datarate = 19.5;
230         }
231       else if (i == 3)
232         {
233           DataRate = StringValue ("HtMcs3");
234           datarate = 26;
235         }
236       else if (i == 4)
237         {
238           DataRate = StringValue ("HtMcs4");
239           datarate = 39;
240         }
241       else if (i == 5)
242         {
243           DataRate = StringValue ("HtMcs5");
244           datarate = 52;
245         }
246       else if (i == 6)
247         {
248           DataRate = StringValue ("HtMcs6");
249           datarate = 58.5;
250         }
251       else if (i == 7)
252         {
253           DataRate = StringValue ("HtMcs7");
254           datarate = 65;
255         }
256       else if (i == 8)
257         {
258           DataRate = StringValue ("HtMcs0");
259           datarate = 7.2;
260         }
261       else if (i == 9)
262         {
263           DataRate = StringValue ("HtMcs1");
264           datarate = 14.4;
265         }
266       else if (i == 10)
267         {
268           DataRate = StringValue ("HtMcs2");
269           datarate = 21.7;
270         }
271       else if (i == 11)
272         {
273           DataRate = StringValue ("HtMcs3");
274           datarate = 28.9;
275         }
276       else if (i == 12)
277         {
278           DataRate = StringValue ("HtMcs4");
279           datarate = 43.3;
280         }
281       else if (i == 13)
282         {
283           DataRate = StringValue ("HtMcs5");
284           datarate = 57.8;
285         }
286       else if (i == 14)
287         {
288           DataRate = StringValue ("HtMcs6");
289           datarate = 65;
290         }
291       else if (i == 15)
292         {
293           DataRate = StringValue ("HtMcs7");
294           datarate = 72.2;
295         }
296       else if (i == 16)
297         {
298           DataRate = StringValue ("HtMcs0");
299           datarate = 13.5;
300         }
301       else if (i == 17)
302         {
303           DataRate = StringValue ("HtMcs1");
304           datarate = 27;
305         }
306       else if (i == 18)
307         {
308           DataRate = StringValue ("HtMcs2");
309           datarate = 40.5;
310         }
311       else if (i == 19)
312         {
313           DataRate = StringValue ("HtMcs3");
314           datarate = 54;
315         }
316       else if (i == 20)
317         {
318           DataRate = StringValue ("HtMcs4");
319           datarate = 81;
320         }
321       else if (i == 21)
322         {
323           DataRate = StringValue ("HtMcs5");
324           datarate = 108;
325         }
326       else if (i == 22)
327         {
328           DataRate = StringValue ("HtMcs6");
329           datarate = 121.5;
330         }
331       else if (i == 23)
332         {
333           DataRate = StringValue ("HtMcs7");
334           datarate = 135;
335         }
336       else if (i == 24)
337         {
338           DataRate = StringValue ("HtMcs0");
339           datarate = 15;
340         }
341       else if (i == 25)
342         {
343           DataRate = StringValue ("HtMcs1");
344           datarate = 30;
345         }
346       else if (i == 26)
347         {
348           DataRate = StringValue ("HtMcs2");
349           datarate = 45;
350         }
351       else if (i == 27)
352         {
353           DataRate = StringValue ("HtMcs3");
354           datarate = 60;
355         }
356       else if (i == 28)
357         {
358           DataRate = StringValue ("HtMcs4");
359           datarate = 90;
360         }
361       else if (i == 29)
362         {
363           DataRate = StringValue ("HtMcs5");
364           datarate = 120;
365         }
366       else if (i == 30)
367         {
368           DataRate = StringValue ("HtMcs6");
369           datarate = 135;
370         }
371       else
372         {
373           DataRate = StringValue ("HtMcs7");
374           datarate = 150;
375         }
376 
377       wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager","DataMode", DataRate,
378                                     "ControlMode", DataRate);
379 
380       NetDeviceContainer staDevice;
381       NetDeviceContainer apDevice;
382 
383       if (wifiType == "ns3::YansWifiPhy")
384         {
385           mac.SetType ("ns3::StaWifiMac",
386                        "Ssid", SsidValue (ssid));
387           phy.Set ("ChannelWidth", UintegerValue (i <= 15 ? 20 : 40));
388           staDevice = wifi.Install (phy, mac, wifiStaNode);
389           mac.SetType ("ns3::ApWifiMac",
390                        "Ssid", SsidValue (ssid));
391           phy.Set ("ChannelWidth", UintegerValue (i <= 15 ? 20 : 40));
392           apDevice = wifi.Install (phy, mac, wifiApNode);
393 
394         }
395       else if (wifiType == "ns3::SpectrumWifiPhy")
396         {
397           mac.SetType ("ns3::StaWifiMac",
398                        "Ssid", SsidValue (ssid));
399           phy.Set ("ChannelWidth", UintegerValue (i <= 15 ? 20 : 40));
400           staDevice = wifi.Install (spectrumPhy, mac, wifiStaNode);
401           mac.SetType ("ns3::ApWifiMac",
402                        "Ssid", SsidValue (ssid));
403           phy.Set ("ChannelWidth", UintegerValue (i <= 15 ? 20 : 40));
404           apDevice = wifi.Install (spectrumPhy, mac, wifiApNode);
405         }
406 
407       if (i <= 7)
408         {
409           Config::Set ("/NodeList/*/DeviceList/*/$ns3::WifiNetDevice/HtConfiguration/ShortGuardIntervalSupported", BooleanValue (false));
410         }
411       else if (i > 7 && i <= 15)
412         {
413           Config::Set ("/NodeList/*/DeviceList/*/$ns3::WifiNetDevice/HtConfiguration/ShortGuardIntervalSupported", BooleanValue (true));
414         }
415       else if (i > 15 && i <= 23)
416         {
417           Config::Set ("/NodeList/*/DeviceList/*/$ns3::WifiNetDevice/HtConfiguration/ShortGuardIntervalSupported", BooleanValue (false));
418         }
419       else
420         {
421           Config::Set ("/NodeList/*/DeviceList/*/$ns3::WifiNetDevice/HtConfiguration/ShortGuardIntervalSupported", BooleanValue (true));
422         }
423 
424       // mobility.
425       MobilityHelper mobility;
426       Ptr<ListPositionAllocator> positionAlloc = CreateObject<ListPositionAllocator> ();
427 
428       positionAlloc->Add (Vector (0.0, 0.0, 0.0));
429       positionAlloc->Add (Vector (distance, 0.0, 0.0));
430       mobility.SetPositionAllocator (positionAlloc);
431 
432       mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
433 
434       mobility.Install (wifiApNode);
435       mobility.Install (wifiStaNode);
436 
437       /* Internet stack*/
438       InternetStackHelper stack;
439       stack.Install (wifiApNode);
440       stack.Install (wifiStaNode);
441 
442       Ipv4AddressHelper address;
443       address.SetBase ("192.168.1.0", "255.255.255.0");
444       Ipv4InterfaceContainer staNodeInterface;
445       Ipv4InterfaceContainer apNodeInterface;
446 
447       staNodeInterface = address.Assign (staDevice);
448       apNodeInterface = address.Assign (apDevice);
449 
450       /* Setting applications */
451       ApplicationContainer serverApp;
452       if (udp)
453         {
454           //UDP flow
455           uint16_t port = 9;
456           UdpServerHelper server (port);
457           serverApp = server.Install (wifiStaNode.Get (0));
458           serverApp.Start (Seconds (0.0));
459           serverApp.Stop (Seconds (simulationTime + 1));
460 
461           UdpClientHelper client (staNodeInterface.GetAddress (0), port);
462           client.SetAttribute ("MaxPackets", UintegerValue (4294967295u));
463           client.SetAttribute ("Interval", TimeValue (Time ("0.0001"))); //packets/s
464           client.SetAttribute ("PacketSize", UintegerValue (payloadSize));
465           ApplicationContainer clientApp = client.Install (wifiApNode.Get (0));
466           clientApp.Start (Seconds (1.0));
467           clientApp.Stop (Seconds (simulationTime + 1));
468         }
469       else
470         {
471           //TCP flow
472           uint16_t port = 50000;
473           Address localAddress (InetSocketAddress (Ipv4Address::GetAny (), port));
474           PacketSinkHelper packetSinkHelper ("ns3::TcpSocketFactory", localAddress);
475           serverApp = packetSinkHelper.Install (wifiStaNode.Get (0));
476           serverApp.Start (Seconds (0.0));
477           serverApp.Stop (Seconds (simulationTime + 1));
478 
479           OnOffHelper onoff ("ns3::TcpSocketFactory", Ipv4Address::GetAny ());
480           onoff.SetAttribute ("OnTime",  StringValue ("ns3::ConstantRandomVariable[Constant=1]"));
481           onoff.SetAttribute ("OffTime", StringValue ("ns3::ConstantRandomVariable[Constant=0]"));
482           onoff.SetAttribute ("PacketSize", UintegerValue (payloadSize));
483           onoff.SetAttribute ("DataRate", DataRateValue (1000000000)); //bit/s
484           AddressValue remoteAddress (InetSocketAddress (staNodeInterface.GetAddress (0), port));
485           onoff.SetAttribute ("Remote", remoteAddress);
486           ApplicationContainer clientApp = onoff.Install (wifiApNode.Get (0));
487           clientApp.Start (Seconds (1.0));
488           clientApp.Stop (Seconds (simulationTime + 1));
489         }
490 
491       Config::ConnectWithoutContext ("/NodeList/0/DeviceList/*/Phy/MonitorSnifferRx", MakeCallback (&MonitorSniffRx));
492 
493       if (enablePcap)
494         {
495           phy.SetPcapDataLinkType (WifiPhyHelper::DLT_IEEE802_11_RADIO);
496           std::stringstream ss;
497           ss << "wifi-spectrum-per-example-" << i;
498           phy.EnablePcap (ss.str (), apDevice);
499         }
500       g_signalDbmAvg = 0;
501       g_noiseDbmAvg = 0;
502       g_samples = 0;
503 
504       Simulator::Stop (Seconds (simulationTime + 1));
505       Simulator::Run ();
506 
507       double throughput = 0;
508       uint64_t totalPacketsThrough = 0;
509       if (udp)
510         {
511           //UDP
512           totalPacketsThrough = DynamicCast<UdpServer> (serverApp.Get (0))->GetReceived ();
513           throughput = totalPacketsThrough * payloadSize * 8 / (simulationTime * 1000000.0); //Mbit/s
514         }
515       else
516         {
517           //TCP
518           uint64_t totalBytesRx = DynamicCast<PacketSink> (serverApp.Get (0))->GetTotalRx ();
519           totalPacketsThrough = totalBytesRx / tcpPacketSize;
520           throughput = totalBytesRx * 8 / (simulationTime * 1000000.0); //Mbit/s
521         }
522       std::cout << std::setw (5) << i <<
523         std::setw (6) << (i % 8) <<
524         std::setprecision (2) << std::fixed <<
525         std::setw (10) << datarate <<
526         std::setw (12) << throughput <<
527         std::setw (8) << totalPacketsThrough;
528       if (totalPacketsThrough > 0)
529         {
530           std::cout << std::setw (12) << g_signalDbmAvg <<
531             std::setw (12) << g_noiseDbmAvg <<
532             std::setw (12) << (g_signalDbmAvg - g_noiseDbmAvg) <<
533             std::endl;
534         }
535       else
536         {
537           std::cout << std::setw (12) << "N/A" <<
538             std::setw (12) << "N/A" <<
539             std::setw (12) << "N/A" <<
540             std::endl;
541         }
542       Simulator::Destroy ();
543     }
544   return 0;
545 }
546