A Discrete-Event Network Simulator
QKDNetSim v2.0 (NS-3 v3.41) @ (+)
API
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
wifi-aggregation.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016 Sébastien Deronne
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  * Author: Sébastien Deronne <sebastien.deronne@gmail.com>
18  */
19 
20 #include "ns3/boolean.h"
21 #include "ns3/command-line.h"
22 #include "ns3/config.h"
23 #include "ns3/internet-stack-helper.h"
24 #include "ns3/ipv4-address-helper.h"
25 #include "ns3/log.h"
26 #include "ns3/mobility-helper.h"
27 #include "ns3/packet-sink-helper.h"
28 #include "ns3/ssid.h"
29 #include "ns3/string.h"
30 #include "ns3/udp-client-server-helper.h"
31 #include "ns3/udp-server.h"
32 #include "ns3/uinteger.h"
33 #include "ns3/wifi-mac.h"
34 #include "ns3/wifi-net-device.h"
35 #include "ns3/yans-wifi-channel.h"
36 #include "ns3/yans-wifi-helper.h"
37 
38 // This is an example that illustrates how 802.11n aggregation is configured.
39 // It defines 4 independent Wi-Fi networks (working on different channels).
40 // Each network contains one access point and one station. Each station
41 // continuously transmits data packets to its respective AP.
42 //
43 // Network topology (numbers in parentheses are channel numbers):
44 //
45 // Network A (36) Network B (40) Network C (44) Network D (48)
46 // * * * * * * * *
47 // | | | | | | | |
48 // AP A STA A AP B STA B AP C STA C AP D STA D
49 //
50 // The aggregation parameters are configured differently on the 4 stations:
51 // - station A uses default aggregation parameter values (A-MSDU disabled, A-MPDU enabled with
52 // maximum size of 65 kB);
53 // - station B doesn't use aggregation (both A-MPDU and A-MSDU are disabled);
54 // - station C enables A-MSDU (with maximum size of 8 kB) but disables A-MPDU;
55 // - station D uses two-level aggregation (A-MPDU with maximum size of 32 kB and A-MSDU with maximum
56 // size of 4 kB).
57 //
58 // Packets in this simulation belong to BestEffort Access Class (AC_BE).
59 //
60 // The user can select the distance between the stations and the APs and can enable/disable the
61 // RTS/CTS mechanism. Example: ./ns3 run "wifi-aggregation --distance=10 --enableRts=0
62 // --simulationTime=20"
63 //
64 // The output prints the throughput measured for the 4 cases/networks described above. When default
65 // aggregation parameters are enabled, the maximum A-MPDU size is 65 kB and the throughput is
66 // maximal. When aggregation is disabled, the throughput is about the half of the physical bitrate.
67 // When only A-MSDU is enabled, the throughput is increased but is not maximal, since the maximum
68 // A-MSDU size is limited to 7935 bytes (whereas the maximum A-MPDU size is limited to 65535 bytes).
69 // When A-MSDU and A-MPDU are both enabled (= two-level aggregation), the throughput is slightly
70 // smaller than the first scenario since we set a smaller maximum A-MPDU size.
71 //
72 // When the distance is increased, the frame error rate gets higher, and the output shows how it
73 // affects the throughput for the 4 networks. Even through A-MSDU has less overheads than A-MPDU,
74 // A-MSDU is less robust against transmission errors than A-MPDU. When the distance is augmented,
75 // the throughput for the third scenario is more affected than the throughput obtained in other
76 // networks.
77 
78 using namespace ns3;
79 
80 NS_LOG_COMPONENT_DEFINE("SimpleMpduAggregation");
81 
82 int
83 main(int argc, char* argv[])
84 {
85  uint32_t payloadSize = 1472; // bytes
86  double simulationTime = 10; // seconds
87  double distance = 5; // meters
88  bool enableRts = false;
89  bool enablePcap = false;
90  bool verifyResults = false; // used for regression
91 
92  CommandLine cmd(__FILE__);
93  cmd.AddValue("payloadSize", "Payload size in bytes", payloadSize);
94  cmd.AddValue("enableRts", "Enable or disable RTS/CTS", enableRts);
95  cmd.AddValue("simulationTime", "Simulation time in seconds", simulationTime);
96  cmd.AddValue("distance",
97  "Distance in meters between the station and the access point",
98  distance);
99  cmd.AddValue("enablePcap", "Enable/disable pcap file generation", enablePcap);
100  cmd.AddValue("verifyResults",
101  "Enable/disable results verification at the end of the simulation",
102  verifyResults);
103  cmd.Parse(argc, argv);
104 
105  Config::SetDefault("ns3::WifiRemoteStationManager::RtsCtsThreshold",
106  enableRts ? StringValue("0") : StringValue("999999"));
107 
109  wifiStaNodes.Create(4);
110  NodeContainer wifiApNodes;
111  wifiApNodes.Create(4);
112 
115  phy.SetPcapDataLinkType(WifiPhyHelper::DLT_IEEE802_11_RADIO);
116  phy.SetChannel(channel.Create());
117 
119  wifi.SetStandard(WIFI_STANDARD_80211n);
120  wifi.SetRemoteStationManager("ns3::ConstantRateWifiManager",
121  "DataMode",
122  StringValue("HtMcs7"),
123  "ControlMode",
124  StringValue("HtMcs0"));
126 
127  NetDeviceContainer staDeviceA;
128  NetDeviceContainer staDeviceB;
129  NetDeviceContainer staDeviceC;
130  NetDeviceContainer staDeviceD;
131  NetDeviceContainer apDeviceA;
132  NetDeviceContainer apDeviceB;
133  NetDeviceContainer apDeviceC;
134  NetDeviceContainer apDeviceD;
135  Ssid ssid;
136 
137  // Network A
138  ssid = Ssid("network-A");
139  phy.Set("ChannelSettings", StringValue("{36, 0, BAND_5GHZ, 0}"));
140  mac.SetType("ns3::StaWifiMac", "Ssid", SsidValue(ssid));
141  staDeviceA = wifi.Install(phy, mac, wifiStaNodes.Get(0));
142 
143  mac.SetType("ns3::ApWifiMac",
144  "Ssid",
145  SsidValue(ssid),
146  "EnableBeaconJitter",
147  BooleanValue(false));
148  apDeviceA = wifi.Install(phy, mac, wifiApNodes.Get(0));
149 
150  // Network B
151  ssid = Ssid("network-B");
152  phy.Set("ChannelSettings", StringValue("{40, 0, BAND_5GHZ, 0}"));
153  mac.SetType("ns3::StaWifiMac", "Ssid", SsidValue(ssid));
154 
155  staDeviceB = wifi.Install(phy, mac, wifiStaNodes.Get(1));
156 
157  // Disable A-MPDU
158  Ptr<NetDevice> dev = wifiStaNodes.Get(1)->GetDevice(0);
159  Ptr<WifiNetDevice> wifi_dev = DynamicCast<WifiNetDevice>(dev);
160  wifi_dev->GetMac()->SetAttribute("BE_MaxAmpduSize", UintegerValue(0));
161 
162  mac.SetType("ns3::ApWifiMac",
163  "Ssid",
164  SsidValue(ssid),
165  "EnableBeaconJitter",
166  BooleanValue(false));
167  apDeviceB = wifi.Install(phy, mac, wifiApNodes.Get(1));
168 
169  // Disable A-MPDU
170  dev = wifiApNodes.Get(1)->GetDevice(0);
171  wifi_dev = DynamicCast<WifiNetDevice>(dev);
172  wifi_dev->GetMac()->SetAttribute("BE_MaxAmpduSize", UintegerValue(0));
173 
174  // Network C
175  ssid = Ssid("network-C");
176  phy.Set("ChannelSettings", StringValue("{44, 0, BAND_5GHZ, 0}"));
177  mac.SetType("ns3::StaWifiMac", "Ssid", SsidValue(ssid));
178 
179  staDeviceC = wifi.Install(phy, mac, wifiStaNodes.Get(2));
180 
181  // Disable A-MPDU and enable A-MSDU with the highest maximum size allowed by the standard (7935
182  // bytes)
183  dev = wifiStaNodes.Get(2)->GetDevice(0);
184  wifi_dev = DynamicCast<WifiNetDevice>(dev);
185  wifi_dev->GetMac()->SetAttribute("BE_MaxAmpduSize", UintegerValue(0));
186  wifi_dev->GetMac()->SetAttribute("BE_MaxAmsduSize", UintegerValue(7935));
187 
188  mac.SetType("ns3::ApWifiMac",
189  "Ssid",
190  SsidValue(ssid),
191  "EnableBeaconJitter",
192  BooleanValue(false));
193  apDeviceC = wifi.Install(phy, mac, wifiApNodes.Get(2));
194 
195  // Disable A-MPDU and enable A-MSDU with the highest maximum size allowed by the standard (7935
196  // bytes)
197  dev = wifiApNodes.Get(2)->GetDevice(0);
198  wifi_dev = DynamicCast<WifiNetDevice>(dev);
199  wifi_dev->GetMac()->SetAttribute("BE_MaxAmpduSize", UintegerValue(0));
200  wifi_dev->GetMac()->SetAttribute("BE_MaxAmsduSize", UintegerValue(7935));
201 
202  // Network D
203  ssid = Ssid("network-D");
204  phy.Set("ChannelSettings", StringValue("{48, 0, BAND_5GHZ, 0}"));
205  mac.SetType("ns3::StaWifiMac", "Ssid", SsidValue(ssid));
206 
207  staDeviceD = wifi.Install(phy, mac, wifiStaNodes.Get(3));
208 
209  // Enable A-MPDU with a smaller size than the default one and
210  // enable A-MSDU with the smallest maximum size allowed by the standard (3839 bytes)
211  dev = wifiStaNodes.Get(3)->GetDevice(0);
212  wifi_dev = DynamicCast<WifiNetDevice>(dev);
213  wifi_dev->GetMac()->SetAttribute("BE_MaxAmpduSize", UintegerValue(32768));
214  wifi_dev->GetMac()->SetAttribute("BE_MaxAmsduSize", UintegerValue(3839));
215 
216  mac.SetType("ns3::ApWifiMac",
217  "Ssid",
218  SsidValue(ssid),
219  "EnableBeaconJitter",
220  BooleanValue(false));
221  apDeviceD = wifi.Install(phy, mac, wifiApNodes.Get(3));
222 
223  // Enable A-MPDU with a smaller size than the default one and
224  // enable A-MSDU with the smallest maximum size allowed by the standard (3839 bytes)
225  dev = wifiApNodes.Get(3)->GetDevice(0);
226  wifi_dev = DynamicCast<WifiNetDevice>(dev);
227  wifi_dev->GetMac()->SetAttribute("BE_MaxAmpduSize", UintegerValue(32768));
228  wifi_dev->GetMac()->SetAttribute("BE_MaxAmsduSize", UintegerValue(3839));
229 
230  // Setting mobility model
232  Ptr<ListPositionAllocator> positionAlloc = CreateObject<ListPositionAllocator>();
233  mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");
234 
235  // Set position for APs
236  positionAlloc->Add(Vector(0.0, 0.0, 0.0));
237  positionAlloc->Add(Vector(10.0, 0.0, 0.0));
238  positionAlloc->Add(Vector(20.0, 0.0, 0.0));
239  positionAlloc->Add(Vector(30.0, 0.0, 0.0));
240  // Set position for STAs
241  positionAlloc->Add(Vector(distance, 0.0, 0.0));
242  positionAlloc->Add(Vector(10 + distance, 0.0, 0.0));
243  positionAlloc->Add(Vector(20 + distance, 0.0, 0.0));
244  positionAlloc->Add(Vector(30 + distance, 0.0, 0.0));
245 
246  mobility.SetPositionAllocator(positionAlloc);
247  mobility.Install(wifiApNodes);
248  mobility.Install(wifiStaNodes);
249 
250  // Internet stack
252  stack.Install(wifiApNodes);
253  stack.Install(wifiStaNodes);
254 
256  address.SetBase("192.168.1.0", "255.255.255.0");
257  Ipv4InterfaceContainer StaInterfaceA;
258  StaInterfaceA = address.Assign(staDeviceA);
259  Ipv4InterfaceContainer ApInterfaceA;
260  ApInterfaceA = address.Assign(apDeviceA);
261 
262  address.SetBase("192.168.2.0", "255.255.255.0");
263  Ipv4InterfaceContainer StaInterfaceB;
264  StaInterfaceB = address.Assign(staDeviceB);
265  Ipv4InterfaceContainer ApInterfaceB;
266  ApInterfaceB = address.Assign(apDeviceB);
267 
268  address.SetBase("192.168.3.0", "255.255.255.0");
269  Ipv4InterfaceContainer StaInterfaceC;
270  StaInterfaceC = address.Assign(staDeviceC);
271  Ipv4InterfaceContainer ApInterfaceC;
272  ApInterfaceC = address.Assign(apDeviceC);
273 
274  address.SetBase("192.168.4.0", "255.255.255.0");
275  Ipv4InterfaceContainer StaInterfaceD;
276  StaInterfaceD = address.Assign(staDeviceD);
277  Ipv4InterfaceContainer ApInterfaceD;
278  ApInterfaceD = address.Assign(apDeviceD);
279 
280  // Setting applications
281  uint16_t port = 9;
282  UdpServerHelper serverA(port);
283  ApplicationContainer serverAppA = serverA.Install(wifiStaNodes.Get(0));
284  serverAppA.Start(Seconds(0.0));
285  serverAppA.Stop(Seconds(simulationTime + 1));
286 
287  UdpClientHelper clientA(StaInterfaceA.GetAddress(0), port);
288  clientA.SetAttribute("MaxPackets", UintegerValue(4294967295U));
289  clientA.SetAttribute("Interval", TimeValue(Time("0.0001"))); // packets/s
290  clientA.SetAttribute("PacketSize", UintegerValue(payloadSize));
291 
292  ApplicationContainer clientAppA = clientA.Install(wifiApNodes.Get(0));
293  clientAppA.Start(Seconds(1.0));
294  clientAppA.Stop(Seconds(simulationTime + 1));
295 
296  UdpServerHelper serverB(port);
297  ApplicationContainer serverAppB = serverB.Install(wifiStaNodes.Get(1));
298  serverAppB.Start(Seconds(0.0));
299  serverAppB.Stop(Seconds(simulationTime + 1));
300 
301  UdpClientHelper clientB(StaInterfaceB.GetAddress(0), port);
302  clientB.SetAttribute("MaxPackets", UintegerValue(4294967295U));
303  clientB.SetAttribute("Interval", TimeValue(Time("0.0001"))); // packets/s
304  clientB.SetAttribute("PacketSize", UintegerValue(payloadSize));
305 
306  ApplicationContainer clientAppB = clientB.Install(wifiApNodes.Get(1));
307  clientAppB.Start(Seconds(1.0));
308  clientAppB.Stop(Seconds(simulationTime + 1));
309 
310  UdpServerHelper serverC(port);
311  ApplicationContainer serverAppC = serverC.Install(wifiStaNodes.Get(2));
312  serverAppC.Start(Seconds(0.0));
313  serverAppC.Stop(Seconds(simulationTime + 1));
314 
315  UdpClientHelper clientC(StaInterfaceC.GetAddress(0), port);
316  clientC.SetAttribute("MaxPackets", UintegerValue(4294967295U));
317  clientC.SetAttribute("Interval", TimeValue(Time("0.0001"))); // packets/s
318  clientC.SetAttribute("PacketSize", UintegerValue(payloadSize));
319 
320  ApplicationContainer clientAppC = clientC.Install(wifiApNodes.Get(2));
321  clientAppC.Start(Seconds(1.0));
322  clientAppC.Stop(Seconds(simulationTime + 1));
323 
324  UdpServerHelper serverD(port);
325  ApplicationContainer serverAppD = serverD.Install(wifiStaNodes.Get(3));
326  serverAppD.Start(Seconds(0.0));
327  serverAppD.Stop(Seconds(simulationTime + 1));
328 
329  UdpClientHelper clientD(StaInterfaceD.GetAddress(0), port);
330  clientD.SetAttribute("MaxPackets", UintegerValue(4294967295U));
331  clientD.SetAttribute("Interval", TimeValue(Time("0.0001"))); // packets/s
332  clientD.SetAttribute("PacketSize", UintegerValue(payloadSize));
333 
334  ApplicationContainer clientAppD = clientD.Install(wifiApNodes.Get(3));
335  clientAppD.Start(Seconds(1.0));
336  clientAppD.Stop(Seconds(simulationTime + 1));
337 
338  if (enablePcap)
339  {
340  phy.EnablePcap("AP_A", apDeviceA.Get(0));
341  phy.EnablePcap("STA_A", staDeviceA.Get(0));
342  phy.EnablePcap("AP_B", apDeviceB.Get(0));
343  phy.EnablePcap("STA_B", staDeviceB.Get(0));
344  phy.EnablePcap("AP_C", apDeviceC.Get(0));
345  phy.EnablePcap("STA_C", staDeviceC.Get(0));
346  phy.EnablePcap("AP_D", apDeviceD.Get(0));
347  phy.EnablePcap("STA_D", staDeviceD.Get(0));
348  }
349 
350  Simulator::Stop(Seconds(simulationTime + 1));
351  Simulator::Run();
352 
353  // Show results
354  uint64_t totalPacketsThroughA = DynamicCast<UdpServer>(serverAppA.Get(0))->GetReceived();
355  uint64_t totalPacketsThroughB = DynamicCast<UdpServer>(serverAppB.Get(0))->GetReceived();
356  uint64_t totalPacketsThroughC = DynamicCast<UdpServer>(serverAppC.Get(0))->GetReceived();
357  uint64_t totalPacketsThroughD = DynamicCast<UdpServer>(serverAppD.Get(0))->GetReceived();
358 
360 
361  double throughput = totalPacketsThroughA * payloadSize * 8 / (simulationTime * 1000000.0);
362  std::cout << "Throughput with default configuration (A-MPDU aggregation enabled, 65kB): "
363  << throughput << " Mbit/s" << '\n';
364  if (verifyResults && (throughput < 59.0 || throughput > 60.0))
365  {
366  NS_LOG_ERROR("Obtained throughput " << throughput << " is not in the expected boundaries!");
367  exit(1);
368  }
369 
370  throughput = totalPacketsThroughB * payloadSize * 8 / (simulationTime * 1000000.0);
371  std::cout << "Throughput with aggregation disabled: " << throughput << " Mbit/s" << '\n';
372  if (verifyResults && (throughput < 30 || throughput > 31))
373  {
374  NS_LOG_ERROR("Obtained throughput " << throughput << " is not in the expected boundaries!");
375  exit(1);
376  }
377 
378  throughput = totalPacketsThroughC * payloadSize * 8 / (simulationTime * 1000000.0);
379  std::cout << "Throughput with A-MPDU disabled and A-MSDU enabled (8kB): " << throughput
380  << " Mbit/s" << '\n';
381  if (verifyResults && (throughput < 51 || throughput > 52))
382  {
383  NS_LOG_ERROR("Obtained throughput " << throughput << " is not in the expected boundaries!");
384  exit(1);
385  }
386 
387  throughput = totalPacketsThroughD * payloadSize * 8 / (simulationTime * 1000000.0);
388  std::cout << "Throughput with A-MPDU enabled (32kB) and A-MSDU enabled (4kB): " << throughput
389  << " Mbit/s" << '\n';
390  if (verifyResults && (throughput < 58 || throughput > 59))
391  {
392  NS_LOG_ERROR("Obtained throughput " << throughput << " is not in the expected boundaries!");
393  exit(1);
394  }
395 
396  return 0;
397 }
holds a vector of ns3::Application pointers.
void Start(Time start) const
Start all of the Applications in this container at the start time given as a parameter.
Ptr< Application > Get(uint32_t i) const
Get the Ptr<Application> stored in this container at a given index.
void Stop(Time stop) const
Arrange for all of the Applications in this container to Stop() at the Time given as a parameter.
Parse command-line arguments.
Definition: command-line.h:232
aggregate IP/TCP/UDP functionality to existing Nodes.
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
holds a vector of std::pair of Ptr<Ipv4> and interface index.
Ipv4Address GetAddress(uint32_t i, uint32_t j=0) const
Helper class used to assign positions and mobility models to nodes.
holds a vector of ns3::NetDevice pointers
Ptr< NetDevice > Get(uint32_t i) const
Get the Ptr<NetDevice> stored in this container at a given index.
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.
Ptr< NetDevice > GetDevice(uint32_t index) const
Retrieve the index-th NetDevice associated to this node.
Definition: node.cc:152
void SetAttribute(std::string name, const AttributeValue &value)
Set a single attribute, raising fatal errors if unsuccessful.
Definition: object-base.cc:204
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:142
static void Run()
Run the simulation.
Definition: simulator.cc:178
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:186
The IEEE 802.11 SSID Information Element.
Definition: ssid.h:36
Hold variables of type string.
Definition: string.h:56
Create a client application which sends UDP packets carrying a 32bit sequence number and a 64 bit tim...
Create a server application which waits for input UDP packets and uses the information carried into t...
Hold an unsigned integer type.
Definition: uinteger.h:45
helps to create WifiNetDevice objects
Definition: wifi-helper.h:324
create MAC layers for a ns3::WifiNetDevice.
Ptr< WifiMac > GetMac() const
@ DLT_IEEE802_11_RADIO
Include Radiotap link layer information.
Definition: wifi-helper.h:178
manage and create wifi channel objects for the YANS model.
static YansWifiChannelHelper Default()
Create a channel helper in a default working state.
Make it easy to create and manage PHY objects for the YANS model.
uint16_t port
Definition: dsdv-manet.cc:44
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:890
#define NS_LOG_ERROR(msg)
Use NS_LOG to output a message of level LOG_ERROR.
Definition: log.h:254
#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:1326
@ WIFI_STANDARD_80211n
address
Definition: first.py:47
stack
Definition: first.py:44
void(* Time)(Time oldValue, Time newValue)
TracedValue callback signature for Time.
Definition: nstime.h:839
Every class exported by the ns3 library is enclosed in the ns3 namespace.
cmd
Definition: second.py:40
ssid
Definition: third.py:93
channel
Definition: third.py:88
mac
Definition: third.py:92
wifi
Definition: third.py:95
mobility
Definition: third.py:105
wifiStaNodes
Definition: third.py:84
phy
Definition: third.py:89
std::ofstream throughput