-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.cpp
436 lines (383 loc) · 15.2 KB
/
main.cpp
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2013 ResiliNets, ITTC, University of Kansas
*
* 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
*
* Authors: Justin P. Rohrer, Truc Anh N. Nguyen <[email protected]>, Siddharth Gangadhar <[email protected]>
*
* James P.G. Sterbenz <[email protected]>, director
* ResiliNets Research Group http://wiki.ittc.ku.edu/resilinets
* Information and Telecommunication Technology Center (ITTC)
* and Department of Electrical Engineering and Computer Science
* The University of Kansas Lawrence, KS USA.
*
* Work supported in part by NSF FIND (Future Internet Design) Program
* under grant CNS-0626918 (Postmodern Internet Architecture),
* NSF grant CNS-1050226 (Multilayer Network Resilience Analysis and Experimentation on GENI),
* US Department of Defense (DoD), and ITTC at The University of Kansas.
*
* “TCP Westwood(+) Protocol Implementation in ns-3”
* Siddharth Gangadhar, Trúc Anh Ngọc Nguyễn , Greeshma Umapathi, and James P.G. Sterbenz,
* ICST SIMUTools Workshop on ns-3 (WNS3), Cannes, France, March 2013
*/
#include <iostream>
#include <fstream>
#include <string>
#include "ns3/core-module.h"
#include "ns3/network-module.h"
#include "ns3/internet-module.h"
#include "ns3/point-to-point-module.h"
#include "ns3/applications-module.h"
#include "ns3/error-model.h"
#include "ns3/tcp-header.h"
#include "ns3/udp-header.h"
#include "ns3/enum.h"
#include "ns3/event-id.h"
#include "ns3/flow-monitor-helper.h"
#include "ns3/ipv4-global-routing-helper.h"
//#include "ns3/tra"
#include "tcp-cubic.h"
using namespace ns3;
NS_LOG_COMPONENT_DEFINE ("TcpVariantsComparison");
bool firstCwnd = true;
bool firstCwnd0 = true;
bool firstSshThr = true;
bool firstRtt = true;
bool firstRto = true;
Ptr<OutputStreamWrapper> cWndStream;
Ptr<OutputStreamWrapper> cWndStream0;
Ptr<OutputStreamWrapper> ssThreshStream;
Ptr<OutputStreamWrapper> rttStream;
Ptr<OutputStreamWrapper> rtoStream;
uint32_t cWndValue = 0;
uint32_t cWndValue0 = 0;
uint32_t ssThreshValue = 0;
static void
CwndTracer (uint32_t oldval, uint32_t newval)
{
//NodeListIndex nodeIndex;
// context
// std::cout<<nodeIndex<<std::endl;
if (firstCwnd)
{
*cWndStream->GetStream () << "0.0 " << oldval << std::endl;
firstCwnd = false;
}
*cWndStream->GetStream () << Simulator::Now ().GetSeconds () << " " << newval << std::endl;
cWndValue = newval;
if (!firstSshThr)
{
*ssThreshStream->GetStream () << Simulator::Now ().GetSeconds () << " " << ssThreshValue << std::endl;
}
}
static void
CwndTracer0 (uint32_t oldval, uint32_t newval)
{
std::cout<<"2nd flow trace!!"<<std::endl;
/*if (firstCwnd0)
{
*cWndStream0->GetStream () << "0.0 " << oldval << std::endl;
firstCwnd0 = false;
}
*cWndStream0->GetStream () << Simulator::Now ().GetSeconds () << " " << newval << std::endl;
cWndValue0 = newval;*/
/*
if (!firstSshThr)
{
*ssThreshStream->GetStream () << Simulator::Now ().GetSeconds () << " " << ssThreshValue << std::endl;
}*/
}
static void
SsThreshTracer (uint32_t oldval, uint32_t newval)
{
if (firstSshThr)
{
*ssThreshStream->GetStream () << "0.0 " << oldval << std::endl;
firstSshThr = false;
}
*ssThreshStream->GetStream () << Simulator::Now ().GetSeconds () << " " << newval << std::endl;
ssThreshValue = newval;
if (!firstCwnd)
{
*cWndStream->GetStream () << Simulator::Now ().GetSeconds () << " " << cWndValue << std::endl;
}
}
static void
RttTracer (Time oldval, Time newval)
{
if (firstRtt)
{
*rttStream->GetStream () << "0.0 " << oldval.GetSeconds () << std::endl;
firstRtt = false;
}
*rttStream->GetStream () << Simulator::Now ().GetSeconds () << " " << newval.GetSeconds () << std::endl;
}
static void
RtoTracer (Time oldval, Time newval)
{
if (firstRto)
{
*rtoStream->GetStream () << "0.0 " << oldval.GetSeconds () << std::endl;
firstRto = false;
}
*rtoStream->GetStream () << Simulator::Now ().GetSeconds () << " " << newval.GetSeconds () << std::endl;
}
static void
TraceCwnd (std::string cwnd_tr_file_name)
{
AsciiTraceHelper ascii;
cWndStream = ascii.CreateFileStream (cwnd_tr_file_name.c_str ());
Config::ConnectWithoutContext ("/NodeList/1/$ns3::TcpL4Protocol/SocketList/0/CongestionWindow", MakeCallback (&CwndTracer));
}
static void
TraceSsThresh (std::string ssthresh_tr_file_name)
{
AsciiTraceHelper ascii;
ssThreshStream = ascii.CreateFileStream (ssthresh_tr_file_name.c_str ());
Config::ConnectWithoutContext ("/NodeList/1/$ns3::TcpL4Protocol/SocketList/0/SlowStartThreshold", MakeCallback (&SsThreshTracer));
}
static void
TraceRtt (std::string rtt_tr_file_name)
{
AsciiTraceHelper ascii;
rttStream = ascii.CreateFileStream (rtt_tr_file_name.c_str ());
Config::ConnectWithoutContext ("/NodeList/1/$ns3::TcpL4Protocol/SocketList/0/RTT", MakeCallback (&RttTracer));
}
static void
TraceRto (std::string rto_tr_file_name)
{
AsciiTraceHelper ascii;
rtoStream = ascii.CreateFileStream (rto_tr_file_name.c_str ());
Config::ConnectWithoutContext ("/NodeList/1/$ns3::TcpL4Protocol/SocketList/0/RTO", MakeCallback (&RtoTracer));
}
int main (int argc, char *argv[])
{
std::string transport_prot = "TcpWestwood";
double error_p = 0.0;
std::string bandwidth = "2Mbps";
std::string delay = "0.01ms";
std::string access_bandwidth = "10Mbps";
std::string access_delay = "45ms";
bool tracing = false;
std::string tr_file_name = "";
std::string cwnd_tr_file_name = "";
std::string ssthresh_tr_file_name = "";
std::string rtt_tr_file_name = "";
std::string rto_tr_file_name = "";
double data_mbytes = 0;
uint32_t mtu_bytes = 400;
uint16_t num_flows = 1;
float duration = 100;
uint32_t run = 0;
bool flow_monitor = true;
std::string flow_monitor_file = "";
CommandLine cmd;
cmd.AddValue ("transport_prot", "Transport protocol to use: TcpTahoe, TcpReno, TcpNewReno, TcpWestwood, TcpWestwoodPlus ", transport_prot);
cmd.AddValue ("error_p", "Packet error rate", error_p);
cmd.AddValue ("bandwidth", "Bottleneck bandwidth", bandwidth);
cmd.AddValue ("delay", "Bottleneck bandwidth", delay);
cmd.AddValue ("access_bandwidth", "Access link bandwidth", access_bandwidth);
cmd.AddValue ("access_delay", "Access link delay", access_delay);
cmd.AddValue ("tracing", "Flag to enable/disable tracing", tracing);
cmd.AddValue ("tr_name", "Name of output trace file", tr_file_name);
cmd.AddValue ("cwnd_tr_name", "Name of output trace file", cwnd_tr_file_name);
cmd.AddValue ("ssthresh_tr_name", "Name of output trace file", ssthresh_tr_file_name);
cmd.AddValue ("rtt_tr_name", "Name of output trace file", rtt_tr_file_name);
cmd.AddValue ("rto_tr_name", "Name of output trace file", rto_tr_file_name);
cmd.AddValue ("data", "Number of Megabytes of data to transmit", data_mbytes);
cmd.AddValue ("mtu", "Size of IP packets to send in bytes", mtu_bytes);
cmd.AddValue ("num_flows", "Number of flows", num_flows);
cmd.AddValue ("duration", "Time to allow flows to run in seconds", duration);
cmd.AddValue ("run", "Run index (for setting repeatable seeds)", run);
cmd.AddValue ("flow_monitor_file", "Flow monitor file path", flow_monitor_file);
cmd.Parse (argc, argv);
flow_monitor = !flow_monitor_file.empty();
SeedManager::SetSeed (1);
SeedManager::SetRun (run);
// User may find it convenient to enable logging
LogComponentEnable("TcpVariantsComparison", LOG_LEVEL_ALL);
//LogComponentEnable("BulkSendApplication", LOG_LEVEL_INFO);
//LogComponentEnable("DropTailQueue", LOG_LEVEL_ALL);
// Calculate the ADU size
Header* temp_header = new Ipv4Header ();
uint32_t ip_header = temp_header->GetSerializedSize ();
NS_LOG_LOGIC ("IP Header size is: " << ip_header);
delete temp_header;
temp_header = new TcpHeader ();
uint32_t tcp_header = temp_header->GetSerializedSize ();
NS_LOG_LOGIC ("TCP Header size is: " << tcp_header);
delete temp_header;
uint32_t tcp_adu_size = mtu_bytes - (ip_header + tcp_header);
NS_LOG_LOGIC ("TCP ADU size is: " << tcp_adu_size);
// Set the simulation start and stop time
float start_time = 0.1;
float stop_time = start_time + duration;
// Select TCP variant
if (transport_prot.compare ("TcpTahoe") == 0)
{
Config::SetDefault ("ns3::TcpL4Protocol::SocketType", TypeIdValue (TcpTahoe::GetTypeId ()));
}
else if (transport_prot.compare ("TcpReno") == 0)
{
Config::SetDefault ("ns3::TcpL4Protocol::SocketType", TypeIdValue (TcpReno::GetTypeId ()));
}
else if (transport_prot.compare ("TcpNewReno") == 0)
{
Config::SetDefault ("ns3::TcpL4Protocol::SocketType", TypeIdValue (TcpNewReno::GetTypeId ()));
}
else if (transport_prot.compare ("TcpWestwood") == 0)
{ // the default protocol type in ns3::TcpWestwood is WESTWOOD
Config::SetDefault ("ns3::TcpL4Protocol::SocketType", TypeIdValue (TcpWestwood::GetTypeId ()));
Config::SetDefault ("ns3::TcpWestwood::FilterType", EnumValue (TcpWestwood::TUSTIN));
}
else if (transport_prot.compare ("TcpWestwoodPlus") == 0)
{
Config::SetDefault ("ns3::TcpL4Protocol::SocketType", TypeIdValue (TcpWestwood::GetTypeId ()));
Config::SetDefault ("ns3::TcpWestwood::ProtocolType", EnumValue (TcpWestwood::WESTWOODPLUS));
Config::SetDefault ("ns3::TcpWestwood::FilterType", EnumValue (TcpWestwood::TUSTIN));
}
else if (transport_prot.compare ("TcpCubic") == 0)
{
Config::SetDefault ("ns3::TcpL4Protocol::SocketType", TypeIdValue (TcpCubic::GetTypeId ()));
}
else
{
NS_LOG_DEBUG ("Invalid TCP version");
exit (1);
}
// Create gateways, sources, and sinks
NodeContainer gateways;
gateways.Create (1);
NodeContainer sources;
sources.Create (num_flows);
NodeContainer sinks;
sinks.Create (num_flows);
// Configure the error model
// Here we use RateErrorModel with packet error rate
Ptr<UniformRandomVariable> uv = CreateObject<UniformRandomVariable> ();
uv->SetStream (50);
RateErrorModel error_model;
error_model.SetRandomVariable (uv);
error_model.SetUnit (RateErrorModel::ERROR_UNIT_PACKET);
error_model.SetRate (error_p);
PointToPointHelper UnReLink;
UnReLink.SetDeviceAttribute ("DataRate", StringValue (bandwidth));
UnReLink.SetChannelAttribute ("Delay", StringValue (delay));
UnReLink.SetDeviceAttribute ("ReceiveErrorModel", PointerValue (&error_model));
InternetStackHelper stack;
stack.InstallAll ();
Ipv4AddressHelper address;
address.SetBase ("10.0.0.0", "255.255.255.0");
// Configure the sources and sinks net devices
// and the channels between the sources/sinks and the gateways
PointToPointHelper LocalLink;
LocalLink.SetDeviceAttribute ("DataRate", StringValue (access_bandwidth));
LocalLink.SetChannelAttribute ("Delay", StringValue (access_delay));
Ipv4InterfaceContainer sink_interfaces;
//std::cout<<"getway ID "<<gateways.Get (0)->GetId ()<<std::endl;
for (int i = 0; i < num_flows; i++)
{
NetDeviceContainer devices;
devices = LocalLink.Install (sources.Get (i), gateways.Get (0));
//std::cout<<"node ID "<<sources.Get (i)->GetId ()<<std::endl;
//std::cout<<"sink ID "<<sinks.Get (i)->GetId ()<<std::endl;
address.NewNetwork ();
//sources.Get (i)->TraceConnect("CongestionWindow",cWndStream0);
Ipv4InterfaceContainer interfaces = address.Assign (devices);
devices = UnReLink.Install (gateways.Get (0), sinks.Get (i));
address.NewNetwork ();
interfaces = address.Assign (devices);
sink_interfaces.Add (interfaces.Get (1));
}
NS_LOG_INFO ("Initialize Global Routing.");
Ipv4GlobalRoutingHelper::PopulateRoutingTables ();
uint16_t port = 50000;
Address sinkLocalAddress (InetSocketAddress (Ipv4Address::GetAny (), port));
PacketSinkHelper sinkHelper ("ns3::TcpSocketFactory", sinkLocalAddress);
for (uint16_t i = 0; i < sources.GetN (); i++)
{
AddressValue remoteAddress (InetSocketAddress (sink_interfaces.GetAddress (i, 0), port));
if (transport_prot.compare ("TcpTahoe") == 0
|| transport_prot.compare ("TcpReno") == 0
|| transport_prot.compare ("TcpNewReno") == 0
|| transport_prot.compare ("TcpWestwood") == 0
|| transport_prot.compare ("TcpWestwoodPlus") == 0
|| transport_prot.compare ("TcpCubic") == 0)
{
Config::SetDefault ("ns3::TcpSocket::SegmentSize", UintegerValue (tcp_adu_size));
BulkSendHelper ftp ("ns3::TcpSocketFactory", Address ());
ftp.SetAttribute ("Remote", remoteAddress);
ftp.SetAttribute ("SendSize", UintegerValue (tcp_adu_size));
ftp.SetAttribute ("MaxBytes", UintegerValue (int(data_mbytes * 1000000)));
ApplicationContainer sourceApp = ftp.Install (sources.Get (i));
sourceApp.Start (Seconds (start_time * i));
sourceApp.Stop (Seconds (stop_time - 3));
sinkHelper.SetAttribute ("Protocol", TypeIdValue (TcpSocketFactory::GetTypeId ()));
ApplicationContainer sinkApp = sinkHelper.Install (sinks);
sinkApp.Start (Seconds (start_time * i));
sinkApp.Stop (Seconds (stop_time));
}
else
{
NS_LOG_DEBUG ("Invalid transport protocol " << transport_prot << " specified");
exit (1);
}
}
// Set up tracing if enabled
if (tracing)
{
if (tr_file_name.compare ("") != 0)
{
std::ofstream ascii;
Ptr<OutputStreamWrapper> ascii_wrap;
ascii.open (tr_file_name.c_str ());
ascii_wrap = new OutputStreamWrapper (tr_file_name.c_str (), std::ios::out);
stack.EnableAsciiIpv4All (ascii_wrap);
}
if (cwnd_tr_file_name.compare ("") != 0)
{
Simulator::Schedule (Seconds (0.00001), &TraceCwnd, cwnd_tr_file_name);
}
if (ssthresh_tr_file_name.compare ("") != 0)
{
Simulator::Schedule (Seconds (0.00001), &TraceSsThresh, ssthresh_tr_file_name);
}
if (rtt_tr_file_name.compare ("") != 0)
{
Simulator::Schedule (Seconds (0.00001), &TraceRtt, rtt_tr_file_name);
}
if (rto_tr_file_name.compare ("") != 0)
{
Simulator::Schedule (Seconds (0.00001), &TraceRto, rto_tr_file_name);
}
}
//UnReLink.EnablePcapAll ("TcpVariantsComparison", true);
//LocalLink.EnablePcapAll ("TcpVariantsComparison", true);
// Flow monitor
FlowMonitorHelper flowHelper;
if (flow_monitor)
{
flowHelper.InstallAll ();
}
Simulator::Stop (Seconds (stop_time));
Simulator::Run ();
if (flow_monitor)
{
std::cout<<"Serializing flow monitor"<<std::endl;
flowHelper.SerializeToXmlFile ( flow_monitor_file, true, true);
}
Simulator::Destroy ();
return 0;
}