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  Time check_start (Seconds((start_time*i)+3));
304 
305  sinkHelper.SetAttribute ("Protocol", TypeIdValue (TcpSocketFactory::GetTypeId ()));
306  ApplicationContainer sinkApp = sinkHelper.Install (sinks);
307  sinkApp.Start (Seconds (start_time*i));
308  sinkApp.Stop (Seconds (stop_time));
309  }
310  else
311  {
312  NS_LOG_DEBUG ("Invalid transport protocol " << transport_prot << " specified");
313  exit (1);
314  }
315  }
316 
317  // Set up tracing if enabled
318  if (tracing)
319  {
320  std::ofstream ascii;
321  Ptr<OutputStreamWrapper> ascii_wrap;
322  if (tr_file_name.compare("") == 0)
323  {
324  NS_LOG_DEBUG ("No trace file provided");
325  exit (1);
326  }
327  else
328  {
329  ascii.open (tr_file_name.c_str());
330  ascii_wrap = new OutputStreamWrapper(tr_file_name.c_str(), std::ios::out);
331  }
332 
333  stack.EnableAsciiIpv4All (ascii_wrap);
334 
335  Simulator::Schedule(Seconds(0.00001), &TraceCwnd, cwnd_tr_file_name);
336  Simulator::Schedule(Seconds(0.00001), &TraceSsThresh, ssthresh_tr_file_name);
337  }
338 
339  UnReLink.EnablePcapAll("TcpVariantsComparison", true);
340  LocalLink.EnablePcapAll("TcpVariantsComparison", true);
341 
342  // Flow monitor
343  Ptr<FlowMonitor> flowMonitor;
344  FlowMonitorHelper flowHelper;
345  if (flow_monitor)
346  {
347  flowMonitor = flowHelper.InstallAll();
348  }
349 
350  Simulator::Stop (Seconds(stop_time));
351  Simulator::Run ();
352 
353  if (flow_monitor)
354  {
355  flowMonitor->SerializeToXmlFile("TcpVariantsComparison.flowmonitor", true, true);
356  }
357 
359  return 0;
360 }