-
Notifications
You must be signed in to change notification settings - Fork 86
/
rate-control.cc
361 lines (297 loc) · 12.6 KB
/
rate-control.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation;
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Based on 'examples/tutorials/third.cc'
* Modify: Xun Deng <[email protected]>
* Hao Yin <[email protected]>
* Muyuan Shen <[email protected]>
*/
#include "ns3/applications-module.h"
#include "ns3/core-module.h"
#include "ns3/csma-module.h"
#include "ns3/flow-monitor-helper.h"
#include "ns3/flow-monitor.h"
#include "ns3/internet-module.h"
#include "ns3/ipv4-flow-classifier.h"
#include "ns3/mobility-module.h"
#include "ns3/network-module.h"
#include "ns3/point-to-point-module.h"
#include "ns3/ssid.h"
#include "ns3/yans-wifi-helper.h"
#include <fstream>
#include <iostream>
using namespace ns3;
NS_LOG_COMPONENT_DEFINE("RateControl");
GlobalValue gThompsonSamplingStream("TSStream",
"Stream Value of Thompson Sampling",
IntegerValue(100),
MakeIntegerChecker<int64_t>());
// This structure stores global variables, which are needed to calculate throughput and delay every
// second
struct DataForThpt
{
FlowMonitorHelper flowmon;
Ptr<FlowMonitor> monitor;
uint32_t totalRxPackets; // Total number of received packets in all flows
uint64_t totalRxBytes; // Total bytes received in all flows
double totalDelaySum; // Total delay sum in all flows
// average delay (ms)
double averageDelay()
{
return totalRxPackets ? totalDelaySum / totalRxPackets / 1000000 : 0;
}
} data; // data is a structure variable which will store all these global variables.
double duration = 5.0; // Duration of simulation (s)
double statInterval = 0.1; // Time interval of calling function Throughput
// This function is being called every 'statInterval' seconds, It measures delay and throughput in
// every 'statInterval' time window. It calculates overall throughput in that window of all flows in
// the network.
static void
Throughput()
{
data.monitor->CheckForLostPackets();
// Ptr<Ipv4FlowClassifier> classifier = DynamicCast<Ipv4FlowClassifier>
// (data.flowmon.GetClassifier ());
const FlowMonitor::FlowStatsContainer stats = data.monitor->GetFlowStats();
uint64_t totalRxBytes = 0;
uint32_t totalRxPackets = 0;
double totalDelaySum = 0;
// Iterating through every flow
for (FlowMonitor::FlowStatsContainerCI iter = stats.begin(); iter != stats.end(); iter++)
{
totalRxBytes += iter->second.rxBytes;
totalDelaySum += iter->second.delaySum.GetDouble();
totalRxPackets += iter->second.rxPackets;
}
uint64_t rxBytesDiff = totalRxBytes - data.totalRxBytes;
uint32_t rxPacketsDiff = totalRxPackets - data.totalRxPackets;
double delayDiff = totalDelaySum - data.totalDelaySum;
data.totalRxBytes = totalRxBytes;
data.totalRxPackets = totalRxPackets;
data.totalDelaySum = totalDelaySum;
double delay = 0.0; // ms
if (rxPacketsDiff != 0 && delayDiff != 0)
{
delay = delayDiff / rxPacketsDiff / 1000000;
}
double tpt = 8.0 * rxBytesDiff / statInterval / (1024 * 1024); // Mbps
std::cout << "Delay: " << delay << "ms, Throughput: " << tpt << "Mbps" << std::endl;
Simulator::Schedule(Seconds(statInterval), &Throughput);
}
int
setWifiStandard(WifiHelper& wifi, const std::string standard)
{
if (standard == "11a")
{
wifi.SetStandard(WIFI_STANDARD_80211a);
}
else if (standard == "11n")
{
wifi.SetStandard(WIFI_STANDARD_80211n);
}
else if (standard == "11ac")
{
wifi.SetStandard(WIFI_STANDARD_80211ac);
}
else if (standard == "11ax")
{
wifi.SetStandard(WIFI_STANDARD_80211ax);
}
else
{
std::cout << "Unknown OFDM standard" << std::endl;
return 1;
}
return 0;
}
int
main(int argc, char* argv[])
{
// LogComponentEnable ("AiThompsonSamplingWifiManager", LOG_LEVEL_ALL);
bool tracing = false;
bool verbose = true;
uint32_t nCsma = 3; // Number of CSMA(LAN) nodes
uint32_t nWifi = 3; // Number of STA(Stations)
uint32_t maxBytes = 0;
std::string errorModelType = "ns3::NistErrorRateModel"; // Error Model
std::string raaAlgo = "MinstrelHt"; // RAA algorithm (WifiManager Class)
std::string standard = "11ac";
// Variables to set rates of various channels in topology, Refer base topology structure.
uint32_t csmaRate = 150;
uint32_t csmaDelay = 9000;
uint32_t p2pRate = 50;
uint32_t p2pDelay = 10;
// Command-Line argument to make it interactive.
CommandLine cmd(__FILE__);
cmd.AddValue("duration", "Duration of simulation (s)", duration);
cmd.AddValue("nCsma", "Number of \"extra\" CSMA nodes/devices", nCsma);
cmd.AddValue("nWifi", "Number of wifi STA devices", nWifi);
cmd.AddValue("verbose", "Tell echo applications to log if true", verbose);
cmd.AddValue("tracing", "Enable pcap tracing", tracing);
cmd.AddValue("raa", "Rate adaptation algorithm, AiConstantRate or AiThompsonSampling", raaAlgo);
cmd.AddValue("maxBytes", "Max number of Bytes to be sent", maxBytes);
cmd.AddValue("p2pRate", "Mbps", p2pRate);
cmd.AddValue("p2pDelay", "MilliSeconds", p2pDelay);
cmd.AddValue("csmaDelay", "NanoSeconds", csmaDelay);
cmd.AddValue("csmaRate", "Mbps", csmaRate);
cmd.AddValue("standard", "WiFi standard", standard);
cmd.Parse(argc, argv);
std::cout << "nWifi: " << nWifi << ", RAA Algorithm: " << raaAlgo << ", duration: " << duration
<< std::endl;
raaAlgo = "ns3::" + raaAlgo + "WifiManager";
// The underlying restriction of 18 is due to the grid position
// allocator's configuration; the grid layout will exceed the
// bounding box if more than 18 nodes are provided.
if (nWifi > 18)
{
std::cout << "nWifi should be 18 or less; otherwise grid layout exceeds the bounding box"
<< std::endl;
return 1;
}
NodeContainer p2pNodes;
p2pNodes.Create(2);
PointToPointHelper pointToPoint;
pointToPoint.SetDeviceAttribute("DataRate", StringValue(std::to_string(p2pRate) + "Mbps"));
pointToPoint.SetChannelAttribute("Delay", StringValue(std::to_string(p2pDelay) + "ms"));
NetDeviceContainer p2pDevices;
p2pDevices = pointToPoint.Install(p2pNodes);
NodeContainer csmaNodes;
csmaNodes.Add(p2pNodes.Get(1));
csmaNodes.Create(nCsma);
CsmaHelper csma;
csma.SetChannelAttribute("DataRate", StringValue(std::to_string(csmaRate) + "Mbps"));
csma.SetChannelAttribute("Delay", TimeValue(NanoSeconds(csmaDelay)));
NetDeviceContainer csmaDevices;
csmaDevices = csma.Install(csmaNodes);
NodeContainer wifiStaNodes;
wifiStaNodes.Create(nWifi);
NodeContainer wifiApNode = p2pNodes.Get(0);
YansWifiChannelHelper channel = YansWifiChannelHelper::Default();
// Delay model
channel.SetPropagationDelay("ns3::ConstantSpeedPropagationDelayModel");
// Loss model
channel.AddPropagationLoss("ns3::LogDistancePropagationLossModel",
"Exponent",
DoubleValue(0.3),
"ReferenceLoss",
DoubleValue(4.0));
YansWifiPhyHelper phy;
phy.SetChannel(channel.Create());
// Error Model
phy.SetErrorRateModel(errorModelType);
WifiHelper wifi;
// Setting Wifi Standard (enum WifiStandard)
setWifiStandard(wifi, standard);
// Setting Raa Algorithm, refer to 'src/wifi/model/rate-control'
wifi.SetRemoteStationManager(raaAlgo);
WifiMacHelper mac;
Ssid ssid = Ssid("ns-3-ssid");
mac.SetType("ns3::StaWifiMac", "Ssid", SsidValue(ssid), "ActiveProbing", BooleanValue(false));
NetDeviceContainer staDevices;
staDevices = wifi.Install(phy, mac, wifiStaNodes);
mac.SetType("ns3::ApWifiMac", "Ssid", SsidValue(ssid));
NetDeviceContainer apDevices;
apDevices = wifi.Install(phy, mac, wifiApNode);
if (raaAlgo == "ns3::ThompsonSamplingWifiManager")
{
IntegerValue ival;
gThompsonSamplingStream.GetValue(ival);
NS_LOG_UNCOND("ThompsonSamplingWifiManager stream " << ival.Get());
wifi.AssignStreams(apDevices, ival.Get());
wifi.AssignStreams(staDevices, ival.Get());
}
MobilityHelper mobility;
mobility.SetPositionAllocator("ns3::GridPositionAllocator",
"MinX",
DoubleValue(0.0),
"MinY",
DoubleValue(0.0),
"DeltaX",
DoubleValue(5.0),
"DeltaY",
DoubleValue(10.0),
"GridWidth",
UintegerValue(3),
"LayoutType",
StringValue("RowFirst"));
// Bounds for the Rectangle Grid
mobility.SetMobilityModel("ns3::RandomWalk2dMobilityModel",
"Speed",
StringValue("ns3::ConstantRandomVariable[Constant=1.0]"),
"Bounds",
RectangleValue(Rectangle(-100, 100, -100, 100)));
mobility.Install(wifiStaNodes);
// Setting Mobility model
mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");
mobility.Install(wifiApNode);
InternetStackHelper stack;
stack.Install(csmaNodes);
stack.Install(wifiApNode);
stack.Install(wifiStaNodes);
Ipv4AddressHelper address;
address.SetBase("10.1.1.0", "255.255.255.0");
Ipv4InterfaceContainer p2pInterfaces;
p2pInterfaces = address.Assign(p2pDevices);
address.SetBase("10.1.2.0", "255.255.255.0");
Ipv4InterfaceContainer csmaInterfaces;
csmaInterfaces = address.Assign(csmaDevices);
address.SetBase("10.1.3.0", "255.255.255.0");
address.Assign(staDevices);
address.Assign(apDevices);
NS_LOG_INFO("Create Applications.");
// Creating a BulkSendApplication and install it on one of the wifi-nodes(except AP)
uint16_t port = 8808; // random port for TCP server listening.
// Setting packetsize (Bytes)
uint32_t packetSize = 1024;
BulkSendHelper source("ns3::TcpSocketFactory",
InetSocketAddress(csmaInterfaces.GetAddress(nCsma), port));
// Set the amount of data to send in bytes. Zero is unlimited.
source.SetAttribute("MaxBytes", UintegerValue(maxBytes));
source.SetAttribute("SendSize", UintegerValue(packetSize));
ApplicationContainer sourceApps;
for (int i = 0; i < int(nWifi); i++)
{
sourceApps.Add(source.Install(wifiStaNodes.Get(i)));
}
sourceApps.Start(Seconds(2.0));
sourceApps.Stop(Seconds(2.0 + duration));
// Creating a PacketSinkApplication and install it on one of the CSMA nodes
PacketSinkHelper sink("ns3::TcpSocketFactory",
InetSocketAddress(csmaInterfaces.GetAddress(nCsma), port));
ApplicationContainer sinkApps = sink.Install(csmaNodes.Get(nCsma));
sinkApps.Start(Seconds(2.0 - 1.0));
sinkApps.Stop(Seconds(2.0 + duration));
Ipv4GlobalRoutingHelper::PopulateRoutingTables();
// Initialisation of global variable which are used for Throughput and Delay Calculation.
data.monitor = data.flowmon.InstallAll();
data.totalDelaySum = 0;
data.totalRxBytes = 0;
data.totalRxPackets = 0;
Simulator::Schedule(Seconds(2.0 - 1.0), &Throughput);
Simulator::Stop(Seconds(2.0 + duration + 1.0));
if (tracing)
{
pointToPoint.EnablePcapAll("third_p2p");
phy.EnablePcap("third_phy", apDevices.Get(0));
csma.EnablePcap("third_csma", csmaDevices.Get(0), true);
}
Simulator::Run();
Ptr<PacketSink> sink1 = DynamicCast<PacketSink>(sinkApps.Get(0));
std::cout << "Total Bytes Received: " << sink1->GetTotalRx() << std::endl;
std::cout << "Average Throughput: " << sink1->GetTotalRx() * 8.0 / duration / (1024 * 1024)
<< "Mbps" << std::endl;
std::cout << "Average Delay: " << data.averageDelay() << "ms" << std::endl;
Simulator::Destroy();
return 0;
}