A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
tcp-variants-comparison.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2013 ResiliNets, ITTC, University of Kansas
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  * Authors: Justin P. Rohrer, Truc Anh N. Nguyen <annguyen@ittc.ku.edu>, Siddharth Gangadhar <siddharth@ittc.ku.edu>
19  *
20  * James P.G. Sterbenz <jpgs@ittc.ku.edu>, director
21  * ResiliNets Research Group http://wiki.ittc.ku.edu/resilinets
22  * Information and Telecommunication Technology Center (ITTC)
23  * and Department of Electrical Engineering and Computer Science
24  * The University of Kansas Lawrence, KS USA.
25  *
26  * Work supported in part by NSF FIND (Future Internet Design) Program
27  * under grant CNS-0626918 (Postmodern Internet Architecture),
28  * NSF grant CNS-1050226 (Multilayer Network Resilience Analysis and Experimentation on GENI),
29  * US Department of Defense (DoD), and ITTC at The University of Kansas.
30  *
31  * “TCP Westwood(+) Protocol Implementation in ns-3”
32  * Siddharth Gangadhar, Trúc Anh Ngọc Nguyễn , Greeshma Umapathi, and James P.G. Sterbenz,
33  * ICST SIMUTools Workshop on ns-3 (WNS3), Cannes, France, March 2013
34  */
35 
36 #include <iostream>
37 #include <fstream>
38 #include <string>
39 
40 #include "ns3/core-module.h"
41 #include "ns3/network-module.h"
42 #include "ns3/internet-module.h"
43 #include "ns3/point-to-point-module.h"
44 #include "ns3/applications-module.h"
45 #include "ns3/error-model.h"
46 #include "ns3/tcp-header.h"
47 #include "ns3/udp-header.h"
48 #include "ns3/enum.h"
49 #include "ns3/event-id.h"
50 #include "ns3/flow-monitor-helper.h"
51 #include "ns3/ipv4-global-routing-helper.h"
52 
53 using namespace ns3;
54 
55 NS_LOG_COMPONENT_DEFINE ("TcpVariantsComparison");
56 
57 double old_time = 0.0;
59 Time current = Time::FromInteger(3, Time::S); //Only record cwnd and ssthresh values every 3 seconds
60 bool first = true;
61 
62 static void
64 {
65  // *stream->GetStream() << newtime << " " << newval << std::endl;
66  // old_time = newval;
67 }
68 
69 static void
70 CwndTracer (Ptr<OutputStreamWrapper>stream, uint32_t oldval, uint32_t newval)
71 {
72  double new_time = Simulator::Now().GetSeconds();
73  if (old_time == 0 && first)
74  {
75  double mycurrent = current.GetSeconds();
76  *stream->GetStream() << new_time << " " << mycurrent << " " << newval << std::endl;
77  first = false;
79  }
80  else
81  {
82  if (output.IsExpired())
83  {
84  *stream->GetStream() << new_time << " " << newval << std::endl;
85  output.Cancel();
87  }
88  }
89 }
90 
91 static void
92 SsThreshTracer (Ptr<OutputStreamWrapper>stream, uint32_t oldval, uint32_t newval)
93 {
94  double new_time = Simulator::Now().GetSeconds();
95  if (old_time == 0 && first)
96  {
97  double mycurrent = current.GetSeconds();
98  *stream->GetStream() << new_time << " " << mycurrent << " " << newval << std::endl;
99  first = false;
101  }
102  else
103  {
104  if (output.IsExpired())
105  {
106  *stream->GetStream() << new_time << " " << newval << std::endl;
107  output.Cancel();
109  }
110  }
111 }
112 
113 static void
114 TraceCwnd (std::string cwnd_tr_file_name)
115 {
116  AsciiTraceHelper ascii;
117  if (cwnd_tr_file_name.compare("") == 0)
118  {
119  NS_LOG_DEBUG ("No trace file for cwnd provided");
120  return;
121  }
122  else
123  {
124  Ptr<OutputStreamWrapper> stream = ascii.CreateFileStream(cwnd_tr_file_name.c_str());
125  Config::ConnectWithoutContext ("/NodeList/1/$ns3::TcpL4Protocol/SocketList/0/CongestionWindow",MakeBoundCallback (&CwndTracer, stream));
126  }
127 }
128 
129 static void
130 TraceSsThresh(std::string ssthresh_tr_file_name)
131 {
132  AsciiTraceHelper ascii;
133  if (ssthresh_tr_file_name.compare("") == 0)
134  {
135  NS_LOG_DEBUG ("No trace file for ssthresh provided");
136  return;
137  }
138  else
139  {
140  Ptr<OutputStreamWrapper> stream = ascii.CreateFileStream(ssthresh_tr_file_name.c_str());
141  Config::ConnectWithoutContext ("/NodeList/1/$ns3::TcpL4Protocol/SocketList/0/SlowStartThreshold",MakeBoundCallback (&SsThreshTracer, stream));
142  }
143 }
144 
145 int main (int argc, char *argv[])
146 {
147  std::string transport_prot = "TcpWestwood";
148  double error_p = 0.0;
149  std::string bandwidth = "2Mbps";
150  std::string access_bandwidth = "10Mbps";
151  std::string access_delay = "45ms";
152  bool tracing = false;
153  std::string tr_file_name = "";
154  std::string cwnd_tr_file_name = "";
155  std::string ssthresh_tr_file_name = "";
156  double data_mbytes = 0;
157  uint32_t mtu_bytes = 400;
158  uint16_t num_flows = 1;
159  float duration = 100;
160  uint32_t run = 0;
161  bool flow_monitor = true;
162 
163 
164  CommandLine cmd;
165  cmd.AddValue("transport_prot", "Transport protocol to use: TcpTahoe, TcpReno, TcpNewReno, TcpWestwood, TcpWestwoodPlus ", transport_prot);
166  cmd.AddValue("error_p", "Packet error rate", error_p);
167  cmd.AddValue("bandwidth", "Bottleneck bandwidth", bandwidth);
168  cmd.AddValue("access_bandwidth", "Access link bandwidth", access_bandwidth);
169  cmd.AddValue("delay", "Access link delay", access_delay);
170  cmd.AddValue("tracing", "Flag to enable/disable tracing", tracing);
171  cmd.AddValue("tr_name", "Name of output trace file", tr_file_name);
172  cmd.AddValue("cwnd_tr_name", "Name of output trace file", cwnd_tr_file_name);
173  cmd.AddValue("ssthresh_tr_name", "Name of output trace file", ssthresh_tr_file_name);
174  cmd.AddValue("data", "Number of Megabytes of data to transmit", data_mbytes);
175  cmd.AddValue("mtu", "Size of IP packets to send in bytes", mtu_bytes);
176  cmd.AddValue("num_flows", "Number of flows", num_flows);
177  cmd.AddValue("duration", "Time to allow flows to run in seconds", duration);
178  cmd.AddValue("run", "Run index (for setting repeatable seeds)", run);
179  cmd.AddValue("flow_monitor", "Enable flow monitor", flow_monitor);
180  cmd.Parse (argc, argv);
181 
183  SeedManager::SetRun(run);
184 
185  // User may find it convenient to enable logging
186  //LogComponentEnable("TcpVariantsComparison", LOG_LEVEL_ALL);
187  //LogComponentEnable("BulkSendApplication", LOG_LEVEL_INFO);
188  //LogComponentEnable("DropTailQueue", LOG_LEVEL_ALL);
189 
190  // Calculate the ADU size
191  Header* temp_header = new Ipv4Header();
192  uint32_t ip_header = temp_header->GetSerializedSize();
193  NS_LOG_LOGIC ("IP Header size is: " << ip_header);
194  delete temp_header;
195  temp_header = new TcpHeader();
196  uint32_t tcp_header = temp_header->GetSerializedSize();
197  NS_LOG_LOGIC ("TCP Header size is: " << tcp_header);
198  delete temp_header;
199  uint32_t tcp_adu_size = mtu_bytes - (ip_header + tcp_header);
200  NS_LOG_LOGIC ("TCP ADU size is: " << tcp_adu_size);
201 
202  // Set the simulation start and stop time
203  float start_time = 0.1;
204  float stop_time = start_time + duration;
205 
206  // Select TCP variant
207  if (transport_prot.compare("TcpTahoe") == 0)
208  Config::SetDefault("ns3::TcpL4Protocol::SocketType", TypeIdValue (TcpTahoe::GetTypeId()));
209  else if (transport_prot.compare("TcpReno") == 0)
210  Config::SetDefault("ns3::TcpL4Protocol::SocketType", TypeIdValue (TcpReno::GetTypeId()));
211  else if (transport_prot.compare("TcpNewReno") == 0)
212  Config::SetDefault("ns3::TcpL4Protocol::SocketType", TypeIdValue (TcpNewReno::GetTypeId()));
213  else if (transport_prot.compare("TcpWestwood") == 0)
214  {// the default protocol type in ns3::TcpWestwood is WESTWOOD
215  Config::SetDefault("ns3::TcpL4Protocol::SocketType", TypeIdValue (TcpWestwood::GetTypeId()));
216  Config::SetDefault("ns3::TcpWestwood::FilterType", EnumValue(TcpWestwood::TUSTIN));
217  }
218  else if (transport_prot.compare("TcpWestwoodPlus") == 0)
219  {
220  Config::SetDefault("ns3::TcpL4Protocol::SocketType", TypeIdValue (TcpWestwood::GetTypeId()));
221  Config::SetDefault("ns3::TcpWestwood::ProtocolType", EnumValue(TcpWestwood::WESTWOODPLUS));
222  Config::SetDefault("ns3::TcpWestwood::FilterType", EnumValue(TcpWestwood::TUSTIN));
223  }
224  else
225  {
226  NS_LOG_DEBUG ("Invalid TCP version");
227  exit (1);
228  }
229 
230  // Create gateways, sources, and sinks
231  NodeContainer gateways;
232  gateways.Create (1);
233  NodeContainer sources;
234  sources.Create(num_flows);
235  NodeContainer sinks;
236  sinks.Create(num_flows);
237 
238  // Configure the error model
239  // Here we use RateErrorModel with packet error rate
240  Ptr<UniformRandomVariable> uv = CreateObject<UniformRandomVariable>();
241  uv->SetStream (50);
242  RateErrorModel error_model;
243  error_model.SetRandomVariable(uv);
245  error_model.SetRate(error_p);
246 
247  PointToPointHelper UnReLink;
248  UnReLink.SetDeviceAttribute ("DataRate", StringValue (bandwidth));
249  UnReLink.SetChannelAttribute ("Delay", StringValue ("0.01ms"));
250  UnReLink.SetDeviceAttribute ("ReceiveErrorModel", PointerValue (&error_model));
251 
252 
254  stack.InstallAll ();
255 
257  address.SetBase ("10.0.0.0", "255.255.255.0");
258 
259  // Configure the sources and sinks net devices
260  // and the channels between the sources/sinks and the gateways
261  PointToPointHelper LocalLink;
262  LocalLink.SetDeviceAttribute ("DataRate", StringValue (access_bandwidth));
263  LocalLink.SetChannelAttribute ("Delay", StringValue (access_delay));
264  Ipv4InterfaceContainer sink_interfaces;
265  for (int i=0; i<num_flows; i++)
266  {
268  devices = LocalLink.Install(sources.Get(i), gateways.Get(0));
269  address.NewNetwork();
270  Ipv4InterfaceContainer interfaces = address.Assign (devices);
271  devices = UnReLink.Install(gateways.Get(0), sinks.Get(i));
272  address.NewNetwork();
273  interfaces = address.Assign (devices);
274  sink_interfaces.Add(interfaces.Get(1));
275  }
276 
277  NS_LOG_INFO ("Initialize Global Routing.");
279 
280  uint16_t port = 50000;
281  Address sinkLocalAddress (InetSocketAddress (Ipv4Address::GetAny (), port));
282  PacketSinkHelper sinkHelper ("ns3::TcpSocketFactory", sinkLocalAddress);
283 
284  for(uint16_t i=0; i<sources.GetN(); i++)
285  {
286  AddressValue remoteAddress (InetSocketAddress (sink_interfaces.GetAddress(i, 0), port));
287 
288  if (transport_prot.compare("TcpTahoe") == 0
289  || transport_prot.compare("TcpReno") == 0
290  || transport_prot.compare("TcpNewReno") == 0
291  || transport_prot.compare("TcpWestwood") == 0
292  || transport_prot.compare("TcpWestwoodPlus") == 0)
293  {
294  Config::SetDefault ("ns3::TcpSocket::SegmentSize", UintegerValue (tcp_adu_size));
295  BulkSendHelper ftp("ns3::TcpSocketFactory", Address());
296  ftp.SetAttribute ("Remote", remoteAddress);
297  ftp.SetAttribute ("SendSize", UintegerValue (tcp_adu_size));
298  ftp.SetAttribute ("MaxBytes", UintegerValue (int(data_mbytes*1000000)));
299 
300  ApplicationContainer sourceApp = ftp.Install (sources.Get(i));
301  sourceApp.Start (Seconds (start_time*i));
302  sourceApp.Stop (Seconds (stop_time - 3));
303 
304  sinkHelper.SetAttribute ("Protocol", TypeIdValue (TcpSocketFactory::GetTypeId ()));
305  ApplicationContainer sinkApp = sinkHelper.Install (sinks);
306  sinkApp.Start (Seconds (start_time*i));
307  sinkApp.Stop (Seconds (stop_time));
308  }
309  else
310  {
311  NS_LOG_DEBUG ("Invalid transport protocol " << transport_prot << " specified");
312  exit (1);
313  }
314  }
315 
316  // Set up tracing if enabled
317  if (tracing)
318  {
319  std::ofstream ascii;
320  Ptr<OutputStreamWrapper> ascii_wrap;
321  if (tr_file_name.compare("") == 0)
322  {
323  NS_LOG_DEBUG ("No trace file provided");
324  exit (1);
325  }
326  else
327  {
328  ascii.open (tr_file_name.c_str());
329  ascii_wrap = new OutputStreamWrapper(tr_file_name.c_str(), std::ios::out);
330  }
331 
332  stack.EnableAsciiIpv4All (ascii_wrap);
333 
334  Simulator::Schedule(Seconds(0.00001), &TraceCwnd, cwnd_tr_file_name);
335  Simulator::Schedule(Seconds(0.00001), &TraceSsThresh, ssthresh_tr_file_name);
336  }
337 
338  UnReLink.EnablePcapAll("TcpVariantsComparison", true);
339  LocalLink.EnablePcapAll("TcpVariantsComparison", true);
340 
341  // Flow monitor
342  Ptr<FlowMonitor> flowMonitor;
343  FlowMonitorHelper flowHelper;
344  if (flow_monitor)
345  {
346  flowMonitor = flowHelper.InstallAll();
347  }
348 
349  Simulator::Stop (Seconds(stop_time));
350  Simulator::Run ();
351 
352  if (flow_monitor)
353  {
354  flowMonitor->SerializeToXmlFile("TcpVariantsComparison.flowmonitor", true, true);
355  }
356 
358  return 0;
359 }
Protocol header serialization and deserialization.
Definition: header.h:42
holds a vector of ns3::Application pointers.
keep track of time values and allow control of global simulation resolution
Definition: nstime.h:81
Manage ASCII trace files for device models.
Definition: trace-helper.h:128
an Inet address class
static Ipv4Address GetAny(void)
void SetStream(int64_t stream)
Specifies the stream number for this RNG stream.
std::pair< Ptr< Ipv4 >, uint32_t > Get(uint32_t i) const
Get the std::pair of an Ptr and interface stored at the location specified by the index...
tuple devices
Definition: first.py:32
A helper to make it easier to instantiate an ns3::BulkSendApplication on a set of nodes...
NS_LOG_COMPONENT_DEFINE("GrantedTimeWindowMpiInterface")
holds a vector of std::pair of Ptr and interface index.
static void PopulateRoutingTables(void)
Build a routing database and initialize the routing tables of the nodes in the simulation.
hold variables of type string
Definition: string.h:19
NetDeviceContainer Install(NodeContainer c)
Callback< R > MakeBoundCallback(R(*fnPtr)(TX), ARG a1)
Build bound Callbacks which take varying numbers of arguments, and potentially returning a value...
Definition: callback.h:1463
double old_time
static void Run(void)
Run the simulation until one of:
Definition: simulator.cc:157
aggregate IP/TCP/UDP functionality to existing Nodes.
#define NS_LOG_INFO(msg)
Definition: log.h:298
A helper to make it easier to instantiate an ns3::PacketSinkApplication on a set of nodes...
Ptr< OutputStreamWrapper > CreateFileStream(std::string filename, std::ios::openmode filemode=std::ios::out)
Create and initialize an output stream object we'll use to write the traced bits. ...
Build a set of PointToPointNetDevice objects.
static EventId Schedule(Time const &time, MEM mem_ptr, OBJ obj)
Schedule an event to expire at the relative time "time" is reached.
Definition: simulator.h:824
void SetDeviceAttribute(std::string name, const AttributeValue &value)
Set an attribute value to be propagated to each NetDevice created by the helper.
static void SetRun(uint64_t run)
Set the run number of simulation.
uint16_t port
Definition: dsdv-manet.cc:44
a polymophic address class
Definition: address.h:86
uint32_t GetN(void) const
Get the number of Ptr stored in this container.
Packet header for IPv4.
Definition: ipv4-header.h:31
double GetSeconds(void) const
Definition: nstime.h:274
ApplicationContainer Install(NodeContainer c) const
Install an ns3::BulkSendApplication on each node of the input container configured with all the attri...
int main(int argc, char *argv[])
static TypeId GetTypeId(void)
Get the type ID.
Definition: tcp-newreno.cc:39
void EnablePcapAll(std::string prefix, bool promiscuous=false)
Enable pcap output on each device (which is of the appropriate type) in the set of all nodes created ...
hold variables of type 'enum'
Definition: enum.h:37
void InstallAll(void) const
Aggregate IPv4, IPv6, UDP, and TCP stacks to all nodes in the simulation.
Hold an unsigned integer type.
Definition: uinteger.h:46
void SetRate(double rate)
Definition: error-model.cc:208
tuple interfaces
Definition: first.py:40
holds a vector of ns3::NetDevice pointers
static TypeId GetTypeId(void)
Get the type ID.
Definition: tcp-tahoe.cc:39
hold objects of type ns3::TypeId
Ptr< FlowMonitor > InstallAll()
Enable flow monitoring on all nodes.
void Start(Time start)
Arrange for all of the Applications in this container to Start() at the Time given as a parameter...
Parse command-line arguments.
Definition: command-line.h:152
#define NS_LOG_LOGIC(msg)
Definition: log.h:368
void EnableAsciiIpv4All(std::string prefix)
Enable ascii trace output on all Ipv4 and interface pairs existing in the set of all nodes created in...
static void Destroy(void)
Every event scheduled by the Simulator::insertAtDestroy method is invoked.
Definition: simulator.cc:121
static TypeId GetTypeId(void)
Get the type ID.
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:667
static void OutputTrace()
keep track of a set of node pointers.
hold objects of type Ptr
Definition: pointer.h:33
static void TraceSsThresh(std::string ssthresh_tr_file_name)
virtual uint32_t GetSerializedSize(void) const =0
Header for the Transmission Control Protocol.
Definition: tcp-header.h:43
static TypeId GetTypeId(void)
Get the type ID.
Definition: tcp-reno.cc:39
static TypeId GetTypeId(void)
Get the type ID.
Definition: tcp-westwood.cc:53
Helper to enable IPv4 flow monitoring on a set of Nodes.
static Time Now(void)
Return the "current simulation time".
Definition: simulator.cc:180
static void SsThreshTracer(Ptr< OutputStreamWrapper >stream, uint32_t oldval, uint32_t newval)
tuple stack
Definition: first.py:34
void SetChannelAttribute(std::string name, const AttributeValue &value)
Set an attribute value to be propagated to each Channel created by the helper.
static void SetSeed(uint32_t seed)
set the seed it will duplicate the seed value 6 times
void SetUnit(enum ErrorUnit error_unit)
Definition: error-model.cc:194
static Time FromInteger(uint64_t value, enum Unit timeUnit)
Definition: nstime.h:392
hold objects of type ns3::Address
Time current
void Stop(Time stop)
Arrange for all of the Applications in this container to Stop() at the Time given as a parameter...
Ipv4InterfaceContainer Assign(const NetDeviceContainer &c)
Assign IP addresses to the net devices specified in the container based on the current network prefix...
an identifier for simulation events.
Definition: event-id.h:46
void AddValue(const std::string &name, const std::string &help, T &value)
Add a program argument, assigning to POD.
Definition: command-line.h:408
static void Stop(void)
If an event invokes this method, it will be the last event scheduled by the Simulator::run method bef...
Definition: simulator.cc:165
void SerializeToXmlFile(std::string fileName, bool enableHistograms, bool enableProbes)
Same as SerializeToXmlStream, but writes to a file instead.
Ptr< Node > Get(uint32_t i) const
Get the Ptr stored in this container at a given index.
#define NS_LOG_DEBUG(msg)
Definition: log.h:289
void SetRandomVariable(Ptr< RandomVariableStream >)
Definition: error-model.cc:215
static void CwndTracer(Ptr< OutputStreamWrapper >stream, uint32_t oldval, uint32_t newval)
void Cancel(void)
This method is syntactic sugar for the ns3::Simulator::cancel method.
Definition: event-id.cc:47
Ipv4Address NewNetwork(void)
Increment the network number and reset the IP address counter to the base value provided in the SetBa...
ApplicationContainer Install(NodeContainer c) const
Install an ns3::PacketSinkApplication on each node of the input container configured with all the att...
void Parse(int argc, char *argv[])
Parse the program arguments.
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
EventId output
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
void Add(Ipv4InterfaceContainer other)
Concatenate the entries in the other container with ours.
second
Definition: nstime.h:93
void SetAttribute(std::string name, const AttributeValue &value)
Helper function used to set the underlying application attributes, not the socket attributes...
tuple address
Definition: first.py:37
Determine which packets are errored corresponding to an underlying distribution, rate, and unit.
Definition: error-model.h:173
bool IsExpired(void) const
This method is syntactic sugar for the ns3::Simulator::isExpired method.
Definition: event-id.cc:53
static void TraceCwnd(std::string cwnd_tr_file_name)
void ConnectWithoutContext(std::string path, const CallbackBase &cb)
Definition: config.cc:717
std::ostream * GetStream(void)
Return a pointer to an ostream previously set in the wrapper.
void SetAttribute(std::string name, const AttributeValue &value)
Helper function used to set the underlying application attributes.
void SetBase(Ipv4Address network, Ipv4Mask mask, Ipv4Address base="0.0.0.1")
Set the base network number, network mask and base address.
Ipv4Address GetAddress(uint32_t i, uint32_t j=0) const