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 bool firstCwnd = true;
58 bool firstSshThr = true;
61 uint32_t cWndValue;
62 uint32_t ssThreshValue;
63 
64 
65 static void
66 CwndTracer (uint32_t oldval, uint32_t newval)
67 {
68  if (firstCwnd)
69  {
70  *cWndStream->GetStream () << "0.0 " << oldval << std::endl;
71  firstCwnd = false;
72  }
73  *cWndStream->GetStream () << Simulator::Now ().GetSeconds () << " " << newval << std::endl;
74  cWndValue = newval;
75 
76  if (!firstSshThr)
77  {
78  *ssThreshStream->GetStream () << Simulator::Now ().GetSeconds () << " " << ssThreshValue << std::endl;
79  }
80 }
81 
82 static void
83 SsThreshTracer (uint32_t oldval, uint32_t newval)
84 {
85  if (firstSshThr)
86  {
87  *ssThreshStream->GetStream () << "0.0 " << oldval << std::endl;
88  firstSshThr = false;
89  }
90  *ssThreshStream->GetStream () << Simulator::Now ().GetSeconds () << " " << newval << std::endl;
91  ssThreshValue = newval;
92 
93  if (!firstCwnd)
94  {
95  *cWndStream->GetStream () << Simulator::Now ().GetSeconds () << " " << cWndValue << std::endl;
96  }
97 }
98 
99 
100 static void
101 TraceCwnd (std::string cwnd_tr_file_name)
102 {
103  AsciiTraceHelper ascii;
104  cWndStream = ascii.CreateFileStream (cwnd_tr_file_name.c_str ());
105  Config::ConnectWithoutContext ("/NodeList/1/$ns3::TcpL4Protocol/SocketList/0/CongestionWindow", MakeCallback (&CwndTracer));
106 }
107 
108 static void
109 TraceSsThresh (std::string ssthresh_tr_file_name)
110 {
111  AsciiTraceHelper ascii;
112  ssThreshStream = ascii.CreateFileStream (ssthresh_tr_file_name.c_str ());
113  Config::ConnectWithoutContext ("/NodeList/1/$ns3::TcpL4Protocol/SocketList/0/SlowStartThreshold",MakeCallback (&SsThreshTracer));
114 }
115 
116 int main (int argc, char *argv[])
117 {
118  std::string transport_prot = "TcpWestwood";
119  double error_p = 0.0;
120  std::string bandwidth = "2Mbps";
121  std::string access_bandwidth = "10Mbps";
122  std::string access_delay = "45ms";
123  bool tracing = false;
124  std::string tr_file_name = "";
125  std::string cwnd_tr_file_name = "";
126  std::string ssthresh_tr_file_name = "";
127  double data_mbytes = 0;
128  uint32_t mtu_bytes = 400;
129  uint16_t num_flows = 1;
130  float duration = 100;
131  uint32_t run = 0;
132  bool flow_monitor = true;
133 
134 
135  CommandLine cmd;
136  cmd.AddValue ("transport_prot", "Transport protocol to use: TcpTahoe, TcpReno, TcpNewReno, TcpWestwood, TcpWestwoodPlus ", transport_prot);
137  cmd.AddValue ("error_p", "Packet error rate", error_p);
138  cmd.AddValue ("bandwidth", "Bottleneck bandwidth", bandwidth);
139  cmd.AddValue ("access_bandwidth", "Access link bandwidth", access_bandwidth);
140  cmd.AddValue ("delay", "Access link delay", access_delay);
141  cmd.AddValue ("tracing", "Flag to enable/disable tracing", tracing);
142  cmd.AddValue ("tr_name", "Name of output trace file", tr_file_name);
143  cmd.AddValue ("cwnd_tr_name", "Name of output trace file", cwnd_tr_file_name);
144  cmd.AddValue ("ssthresh_tr_name", "Name of output trace file", ssthresh_tr_file_name);
145  cmd.AddValue ("data", "Number of Megabytes of data to transmit", data_mbytes);
146  cmd.AddValue ("mtu", "Size of IP packets to send in bytes", mtu_bytes);
147  cmd.AddValue ("num_flows", "Number of flows", num_flows);
148  cmd.AddValue ("duration", "Time to allow flows to run in seconds", duration);
149  cmd.AddValue ("run", "Run index (for setting repeatable seeds)", run);
150  cmd.AddValue ("flow_monitor", "Enable flow monitor", flow_monitor);
151  cmd.Parse (argc, argv);
152 
154  SeedManager::SetRun (run);
155 
156  // User may find it convenient to enable logging
157  //LogComponentEnable("TcpVariantsComparison", LOG_LEVEL_ALL);
158  //LogComponentEnable("BulkSendApplication", LOG_LEVEL_INFO);
159  //LogComponentEnable("DropTailQueue", LOG_LEVEL_ALL);
160 
161  // Calculate the ADU size
162  Header* temp_header = new Ipv4Header ();
163  uint32_t ip_header = temp_header->GetSerializedSize ();
164  NS_LOG_LOGIC ("IP Header size is: " << ip_header);
165  delete temp_header;
166  temp_header = new TcpHeader ();
167  uint32_t tcp_header = temp_header->GetSerializedSize ();
168  NS_LOG_LOGIC ("TCP Header size is: " << tcp_header);
169  delete temp_header;
170  uint32_t tcp_adu_size = mtu_bytes - (ip_header + tcp_header);
171  NS_LOG_LOGIC ("TCP ADU size is: " << tcp_adu_size);
172 
173  // Set the simulation start and stop time
174  float start_time = 0.1;
175  float stop_time = start_time + duration;
176 
177  // Select TCP variant
178  if (transport_prot.compare ("TcpTahoe") == 0)
179  {
180  Config::SetDefault ("ns3::TcpL4Protocol::SocketType", TypeIdValue (TcpTahoe::GetTypeId ()));
181  }
182  else if (transport_prot.compare ("TcpReno") == 0)
183  {
184  Config::SetDefault ("ns3::TcpL4Protocol::SocketType", TypeIdValue (TcpReno::GetTypeId ()));
185  }
186  else if (transport_prot.compare ("TcpNewReno") == 0)
187  {
188  Config::SetDefault ("ns3::TcpL4Protocol::SocketType", TypeIdValue (TcpNewReno::GetTypeId ()));
189  }
190  else if (transport_prot.compare ("TcpWestwood") == 0)
191  { // the default protocol type in ns3::TcpWestwood is WESTWOOD
192  Config::SetDefault ("ns3::TcpL4Protocol::SocketType", TypeIdValue (TcpWestwood::GetTypeId ()));
193  Config::SetDefault ("ns3::TcpWestwood::FilterType", EnumValue (TcpWestwood::TUSTIN));
194  }
195  else if (transport_prot.compare ("TcpWestwoodPlus") == 0)
196  {
197  Config::SetDefault ("ns3::TcpL4Protocol::SocketType", TypeIdValue (TcpWestwood::GetTypeId ()));
198  Config::SetDefault ("ns3::TcpWestwood::ProtocolType", EnumValue (TcpWestwood::WESTWOODPLUS));
199  Config::SetDefault ("ns3::TcpWestwood::FilterType", EnumValue (TcpWestwood::TUSTIN));
200  }
201  else
202  {
203  NS_LOG_DEBUG ("Invalid TCP version");
204  exit (1);
205  }
206 
207  // Create gateways, sources, and sinks
208  NodeContainer gateways;
209  gateways.Create (1);
210  NodeContainer sources;
211  sources.Create (num_flows);
212  NodeContainer sinks;
213  sinks.Create (num_flows);
214 
215  // Configure the error model
216  // Here we use RateErrorModel with packet error rate
217  Ptr<UniformRandomVariable> uv = CreateObject<UniformRandomVariable> ();
218  uv->SetStream (50);
219  RateErrorModel error_model;
220  error_model.SetRandomVariable (uv);
222  error_model.SetRate (error_p);
223 
224  PointToPointHelper UnReLink;
225  UnReLink.SetDeviceAttribute ("DataRate", StringValue (bandwidth));
226  UnReLink.SetChannelAttribute ("Delay", StringValue ("0.01ms"));
227  UnReLink.SetDeviceAttribute ("ReceiveErrorModel", PointerValue (&error_model));
228 
229 
231  stack.InstallAll ();
232 
234  address.SetBase ("10.0.0.0", "255.255.255.0");
235 
236  // Configure the sources and sinks net devices
237  // and the channels between the sources/sinks and the gateways
238  PointToPointHelper LocalLink;
239  LocalLink.SetDeviceAttribute ("DataRate", StringValue (access_bandwidth));
240  LocalLink.SetChannelAttribute ("Delay", StringValue (access_delay));
241  Ipv4InterfaceContainer sink_interfaces;
242  for (int i = 0; i < num_flows; i++)
243  {
245  devices = LocalLink.Install (sources.Get (i), gateways.Get (0));
246  address.NewNetwork ();
247  Ipv4InterfaceContainer interfaces = address.Assign (devices);
248  devices = UnReLink.Install (gateways.Get (0), sinks.Get (i));
249  address.NewNetwork ();
250  interfaces = address.Assign (devices);
251  sink_interfaces.Add (interfaces.Get (1));
252  }
253 
254  NS_LOG_INFO ("Initialize Global Routing.");
256 
257  uint16_t port = 50000;
258  Address sinkLocalAddress (InetSocketAddress (Ipv4Address::GetAny (), port));
259  PacketSinkHelper sinkHelper ("ns3::TcpSocketFactory", sinkLocalAddress);
260 
261  for (uint16_t i = 0; i < sources.GetN (); i++)
262  {
263  AddressValue remoteAddress (InetSocketAddress (sink_interfaces.GetAddress (i, 0), port));
264 
265  if (transport_prot.compare ("TcpTahoe") == 0
266  || transport_prot.compare ("TcpReno") == 0
267  || transport_prot.compare ("TcpNewReno") == 0
268  || transport_prot.compare ("TcpWestwood") == 0
269  || transport_prot.compare ("TcpWestwoodPlus") == 0)
270  {
271  Config::SetDefault ("ns3::TcpSocket::SegmentSize", UintegerValue (tcp_adu_size));
272  BulkSendHelper ftp ("ns3::TcpSocketFactory", Address ());
273  ftp.SetAttribute ("Remote", remoteAddress);
274  ftp.SetAttribute ("SendSize", UintegerValue (tcp_adu_size));
275  ftp.SetAttribute ("MaxBytes", UintegerValue (int(data_mbytes * 1000000)));
276 
277  ApplicationContainer sourceApp = ftp.Install (sources.Get (i));
278  sourceApp.Start (Seconds (start_time * i));
279  sourceApp.Stop (Seconds (stop_time - 3));
280 
281  sinkHelper.SetAttribute ("Protocol", TypeIdValue (TcpSocketFactory::GetTypeId ()));
282  ApplicationContainer sinkApp = sinkHelper.Install (sinks);
283  sinkApp.Start (Seconds (start_time * i));
284  sinkApp.Stop (Seconds (stop_time));
285  }
286  else
287  {
288  NS_LOG_DEBUG ("Invalid transport protocol " << transport_prot << " specified");
289  exit (1);
290  }
291  }
292 
293  // Set up tracing if enabled
294  if (tracing)
295  {
296  if (tr_file_name.compare ("") != 0)
297  {
298  std::ofstream ascii;
299  Ptr<OutputStreamWrapper> ascii_wrap;
300  ascii.open (tr_file_name.c_str ());
301  ascii_wrap = new OutputStreamWrapper (tr_file_name.c_str (), std::ios::out);
302  stack.EnableAsciiIpv4All (ascii_wrap);
303  }
304 
305  if (cwnd_tr_file_name.compare ("") != 0)
306  {
307  Simulator::Schedule (Seconds (0.00001), &TraceCwnd, cwnd_tr_file_name);
308  }
309 
310  if (ssthresh_tr_file_name.compare ("") != 0)
311  {
312  Simulator::Schedule (Seconds (0.00001), &TraceSsThresh, ssthresh_tr_file_name);
313  }
314 
315  }
316 
317  UnReLink.EnablePcapAll ("TcpVariantsComparison", true);
318  LocalLink.EnablePcapAll ("TcpVariantsComparison", true);
319 
320  // Flow monitor
321  FlowMonitorHelper flowHelper;
322  if (flow_monitor)
323  {
324  flowHelper.InstallAll ();
325  }
326 
327  Simulator::Stop (Seconds (stop_time));
328  Simulator::Run ();
329 
330  if (flow_monitor)
331  {
332  flowHelper.SerializeToXmlFile ("TcpVariantsComparison.flowmonitor", true, true);
333  }
334 
336  return 0;
337 }
Protocol header serialization and deserialization.
Definition: header.h:42
holds a vector of ns3::Application pointers.
Manage ASCII trace files for device models.
Definition: trace-helper.h:141
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...
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:18
NetDeviceContainer Install(NodeContainer c)
uint32_t ssThreshValue
uint32_t cWndValue
static void Run(void)
Run the simulation until one of:
Definition: simulator.cc:157
Ptr< OutputStreamWrapper > cWndStream
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:170
aggregate IP/TCP/UDP functionality to existing Nodes.
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:223
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:825
void SetDeviceAttribute(std::string name, const AttributeValue &value)
Set an attribute value to be propagated to each NetDevice created by the helper.
void SerializeToXmlFile(std::string fileName, bool enableHistograms, bool enableProbes)
Same as SerializeToXmlStream, but writes to a file instead.
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
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:322
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:38
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:206
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:38
A class encapsulating an output stream.
hold objects of type ns3::TypeId
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1283
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:196
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition: log.h:233
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 void CwndTracer(uint32_t oldval, uint32_t newval)
static TypeId GetTypeId(void)
Get the type ID.
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:677
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:44
static TypeId GetTypeId(void)
Get the type ID.
Definition: tcp-reno.cc:38
static TypeId GetTypeId(void)
Get the type ID.
Definition: tcp-westwood.cc:52
Helper to enable IP flow monitoring on a set of Nodes.
bool firstSshThr
static Time Now(void)
Return the "current simulation time".
Definition: simulator.cc:180
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:192
hold objects of type ns3::Address
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...
void AddValue(const std::string &name, const std::string &help, T &value)
Add a program argument, assigning to POD.
Definition: command-line.h:471
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
Ptr< Node > Get(uint32_t i) const
Get the Ptr stored in this container at a given index.
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:213
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:845
bool firstCwnd
void SetRandomVariable(Ptr< RandomVariableStream >)
Definition: error-model.cc:213
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.
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.
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:182
static void TraceCwnd(std::string cwnd_tr_file_name)
static void SsThreshTracer(uint32_t oldval, uint32_t newval)
void ConnectWithoutContext(std::string path, const CallbackBase &cb)
Definition: config.cc:727
std::ostream * GetStream(void)
Return a pointer to an ostream previously set in the wrapper.
Ptr< OutputStreamWrapper > ssThreshStream
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