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 #include "ns3/traffic-control-module.h"
53
54 using namespace ns3;
55
56 NS_LOG_COMPONENT_DEFINE ("TcpVariantsComparison");
57
58 static bool firstCwnd = true;
59 static bool firstSshThr = true;
60 static bool firstRtt = true;
61 static bool firstRto = true;
62 static Ptr<OutputStreamWrapper> cWndStream;
63 static Ptr<OutputStreamWrapper> ssThreshStream;
64 static Ptr<OutputStreamWrapper> rttStream;
65 static Ptr<OutputStreamWrapper> rtoStream;
66 static Ptr<OutputStreamWrapper> nextTxStream;
67 static Ptr<OutputStreamWrapper> nextRxStream;
68 static Ptr<OutputStreamWrapper> inFlightStream;
69 static uint32_t cWndValue;
70 static uint32_t ssThreshValue;
71
72
73 static void
CwndTracer(uint32_t oldval,uint32_t newval)74 CwndTracer (uint32_t oldval, uint32_t newval)
75 {
76 if (firstCwnd)
77 {
78 *cWndStream->GetStream () << "0.0 " << oldval << std::endl;
79 firstCwnd = false;
80 }
81 *cWndStream->GetStream () << Simulator::Now ().GetSeconds () << " " << newval << std::endl;
82 cWndValue = newval;
83
84 if (!firstSshThr)
85 {
86 *ssThreshStream->GetStream () << Simulator::Now ().GetSeconds () << " " << ssThreshValue << std::endl;
87 }
88 }
89
90 static void
SsThreshTracer(uint32_t oldval,uint32_t newval)91 SsThreshTracer (uint32_t oldval, uint32_t newval)
92 {
93 if (firstSshThr)
94 {
95 *ssThreshStream->GetStream () << "0.0 " << oldval << std::endl;
96 firstSshThr = false;
97 }
98 *ssThreshStream->GetStream () << Simulator::Now ().GetSeconds () << " " << newval << std::endl;
99 ssThreshValue = newval;
100
101 if (!firstCwnd)
102 {
103 *cWndStream->GetStream () << Simulator::Now ().GetSeconds () << " " << cWndValue << std::endl;
104 }
105 }
106
107 static void
RttTracer(Time oldval,Time newval)108 RttTracer (Time oldval, Time newval)
109 {
110 if (firstRtt)
111 {
112 *rttStream->GetStream () << "0.0 " << oldval.GetSeconds () << std::endl;
113 firstRtt = false;
114 }
115 *rttStream->GetStream () << Simulator::Now ().GetSeconds () << " " << newval.GetSeconds () << std::endl;
116 }
117
118 static void
RtoTracer(Time oldval,Time newval)119 RtoTracer (Time oldval, Time newval)
120 {
121 if (firstRto)
122 {
123 *rtoStream->GetStream () << "0.0 " << oldval.GetSeconds () << std::endl;
124 firstRto = false;
125 }
126 *rtoStream->GetStream () << Simulator::Now ().GetSeconds () << " " << newval.GetSeconds () << std::endl;
127 }
128
129 static void
NextTxTracer(SequenceNumber32 old,SequenceNumber32 nextTx)130 NextTxTracer (SequenceNumber32 old, SequenceNumber32 nextTx)
131 {
132 NS_UNUSED (old);
133 *nextTxStream->GetStream () << Simulator::Now ().GetSeconds () << " " << nextTx << std::endl;
134 }
135
136 static void
InFlightTracer(uint32_t old,uint32_t inFlight)137 InFlightTracer (uint32_t old, uint32_t inFlight)
138 {
139 NS_UNUSED (old);
140 *inFlightStream->GetStream () << Simulator::Now ().GetSeconds () << " " << inFlight << std::endl;
141 }
142
143 static void
NextRxTracer(SequenceNumber32 old,SequenceNumber32 nextRx)144 NextRxTracer (SequenceNumber32 old, SequenceNumber32 nextRx)
145 {
146 NS_UNUSED (old);
147 *nextRxStream->GetStream () << Simulator::Now ().GetSeconds () << " " << nextRx << std::endl;
148 }
149
150 static void
TraceCwnd(std::string cwnd_tr_file_name)151 TraceCwnd (std::string cwnd_tr_file_name)
152 {
153 AsciiTraceHelper ascii;
154 cWndStream = ascii.CreateFileStream (cwnd_tr_file_name.c_str ());
155 Config::ConnectWithoutContext ("/NodeList/1/$ns3::TcpL4Protocol/SocketList/0/CongestionWindow", MakeCallback (&CwndTracer));
156 }
157
158 static void
TraceSsThresh(std::string ssthresh_tr_file_name)159 TraceSsThresh (std::string ssthresh_tr_file_name)
160 {
161 AsciiTraceHelper ascii;
162 ssThreshStream = ascii.CreateFileStream (ssthresh_tr_file_name.c_str ());
163 Config::ConnectWithoutContext ("/NodeList/1/$ns3::TcpL4Protocol/SocketList/0/SlowStartThreshold", MakeCallback (&SsThreshTracer));
164 }
165
166 static void
TraceRtt(std::string rtt_tr_file_name)167 TraceRtt (std::string rtt_tr_file_name)
168 {
169 AsciiTraceHelper ascii;
170 rttStream = ascii.CreateFileStream (rtt_tr_file_name.c_str ());
171 Config::ConnectWithoutContext ("/NodeList/1/$ns3::TcpL4Protocol/SocketList/0/RTT", MakeCallback (&RttTracer));
172 }
173
174 static void
TraceRto(std::string rto_tr_file_name)175 TraceRto (std::string rto_tr_file_name)
176 {
177 AsciiTraceHelper ascii;
178 rtoStream = ascii.CreateFileStream (rto_tr_file_name.c_str ());
179 Config::ConnectWithoutContext ("/NodeList/1/$ns3::TcpL4Protocol/SocketList/0/RTO", MakeCallback (&RtoTracer));
180 }
181
182 static void
TraceNextTx(std::string & next_tx_seq_file_name)183 TraceNextTx (std::string &next_tx_seq_file_name)
184 {
185 AsciiTraceHelper ascii;
186 nextTxStream = ascii.CreateFileStream (next_tx_seq_file_name.c_str ());
187 Config::ConnectWithoutContext ("/NodeList/1/$ns3::TcpL4Protocol/SocketList/0/NextTxSequence", MakeCallback (&NextTxTracer));
188 }
189
190 static void
TraceInFlight(std::string & in_flight_file_name)191 TraceInFlight (std::string &in_flight_file_name)
192 {
193 AsciiTraceHelper ascii;
194 inFlightStream = ascii.CreateFileStream (in_flight_file_name.c_str ());
195 Config::ConnectWithoutContext ("/NodeList/1/$ns3::TcpL4Protocol/SocketList/0/BytesInFlight", MakeCallback (&InFlightTracer));
196 }
197
198
199 static void
TraceNextRx(std::string & next_rx_seq_file_name)200 TraceNextRx (std::string &next_rx_seq_file_name)
201 {
202 AsciiTraceHelper ascii;
203 nextRxStream = ascii.CreateFileStream (next_rx_seq_file_name.c_str ());
204 Config::ConnectWithoutContext ("/NodeList/2/$ns3::TcpL4Protocol/SocketList/1/RxBuffer/NextRxSequence", MakeCallback (&NextRxTracer));
205 }
206
main(int argc,char * argv[])207 int main (int argc, char *argv[])
208 {
209 std::string transport_prot = "TcpWestwood";
210 double error_p = 0.0;
211 std::string bandwidth = "2Mbps";
212 std::string delay = "0.01ms";
213 std::string access_bandwidth = "10Mbps";
214 std::string access_delay = "45ms";
215 bool tracing = false;
216 std::string prefix_file_name = "TcpVariantsComparison";
217 uint64_t data_mbytes = 0;
218 uint32_t mtu_bytes = 400;
219 uint16_t num_flows = 1;
220 double duration = 100.0;
221 uint32_t run = 0;
222 bool flow_monitor = false;
223 bool pcap = false;
224 bool sack = true;
225 std::string queue_disc_type = "ns3::PfifoFastQueueDisc";
226 std::string recovery = "ns3::TcpClassicRecovery";
227
228
229 CommandLine cmd (__FILE__);
230 cmd.AddValue ("transport_prot", "Transport protocol to use: TcpNewReno, TcpLinuxReno, "
231 "TcpHybla, TcpHighSpeed, TcpHtcp, TcpVegas, TcpScalable, TcpVeno, "
232 "TcpBic, TcpYeah, TcpIllinois, TcpWestwood, TcpWestwoodPlus, TcpLedbat, "
233 "TcpLp, TcpDctcp, TcpCubic, TcpBbr", transport_prot);
234 cmd.AddValue ("error_p", "Packet error rate", error_p);
235 cmd.AddValue ("bandwidth", "Bottleneck bandwidth", bandwidth);
236 cmd.AddValue ("delay", "Bottleneck delay", delay);
237 cmd.AddValue ("access_bandwidth", "Access link bandwidth", access_bandwidth);
238 cmd.AddValue ("access_delay", "Access link delay", access_delay);
239 cmd.AddValue ("tracing", "Flag to enable/disable tracing", tracing);
240 cmd.AddValue ("prefix_name", "Prefix of output trace file", prefix_file_name);
241 cmd.AddValue ("data", "Number of Megabytes of data to transmit", data_mbytes);
242 cmd.AddValue ("mtu", "Size of IP packets to send in bytes", mtu_bytes);
243 cmd.AddValue ("num_flows", "Number of flows", num_flows);
244 cmd.AddValue ("duration", "Time to allow flows to run in seconds", duration);
245 cmd.AddValue ("run", "Run index (for setting repeatable seeds)", run);
246 cmd.AddValue ("flow_monitor", "Enable flow monitor", flow_monitor);
247 cmd.AddValue ("pcap_tracing", "Enable or disable PCAP tracing", pcap);
248 cmd.AddValue ("queue_disc_type", "Queue disc type for gateway (e.g. ns3::CoDelQueueDisc)", queue_disc_type);
249 cmd.AddValue ("sack", "Enable or disable SACK option", sack);
250 cmd.AddValue ("recovery", "Recovery algorithm type to use (e.g., ns3::TcpPrrRecovery", recovery);
251 cmd.Parse (argc, argv);
252
253 transport_prot = std::string ("ns3::") + transport_prot;
254
255 SeedManager::SetSeed (1);
256 SeedManager::SetRun (run);
257
258 // User may find it convenient to enable logging
259 //LogComponentEnable("TcpVariantsComparison", LOG_LEVEL_ALL);
260 //LogComponentEnable("BulkSendApplication", LOG_LEVEL_INFO);
261 //LogComponentEnable("PfifoFastQueueDisc", LOG_LEVEL_ALL);
262
263 // Calculate the ADU size
264 Header* temp_header = new Ipv4Header ();
265 uint32_t ip_header = temp_header->GetSerializedSize ();
266 NS_LOG_LOGIC ("IP Header size is: " << ip_header);
267 delete temp_header;
268 temp_header = new TcpHeader ();
269 uint32_t tcp_header = temp_header->GetSerializedSize ();
270 NS_LOG_LOGIC ("TCP Header size is: " << tcp_header);
271 delete temp_header;
272 uint32_t tcp_adu_size = mtu_bytes - 20 - (ip_header + tcp_header);
273 NS_LOG_LOGIC ("TCP ADU size is: " << tcp_adu_size);
274
275 // Set the simulation start and stop time
276 double start_time = 0.1;
277 double stop_time = start_time + duration;
278
279 // 2 MB of TCP buffer
280 Config::SetDefault ("ns3::TcpSocket::RcvBufSize", UintegerValue (1 << 21));
281 Config::SetDefault ("ns3::TcpSocket::SndBufSize", UintegerValue (1 << 21));
282 Config::SetDefault ("ns3::TcpSocketBase::Sack", BooleanValue (sack));
283
284 Config::SetDefault ("ns3::TcpL4Protocol::RecoveryType",
285 TypeIdValue (TypeId::LookupByName (recovery)));
286 // Select TCP variant
287 if (transport_prot.compare ("ns3::TcpWestwoodPlus") == 0)
288 {
289 // TcpWestwoodPlus is not an actual TypeId name; we need TcpWestwood here
290 Config::SetDefault ("ns3::TcpL4Protocol::SocketType", TypeIdValue (TcpWestwood::GetTypeId ()));
291 // the default protocol type in ns3::TcpWestwood is WESTWOOD
292 Config::SetDefault ("ns3::TcpWestwood::ProtocolType", EnumValue (TcpWestwood::WESTWOODPLUS));
293 }
294 else
295 {
296 TypeId tcpTid;
297 NS_ABORT_MSG_UNLESS (TypeId::LookupByNameFailSafe (transport_prot, &tcpTid), "TypeId " << transport_prot << " not found");
298 Config::SetDefault ("ns3::TcpL4Protocol::SocketType", TypeIdValue (TypeId::LookupByName (transport_prot)));
299 }
300
301 // Create gateways, sources, and sinks
302 NodeContainer gateways;
303 gateways.Create (1);
304 NodeContainer sources;
305 sources.Create (num_flows);
306 NodeContainer sinks;
307 sinks.Create (num_flows);
308
309 // Configure the error model
310 // Here we use RateErrorModel with packet error rate
311 Ptr<UniformRandomVariable> uv = CreateObject<UniformRandomVariable> ();
312 uv->SetStream (50);
313 RateErrorModel error_model;
314 error_model.SetRandomVariable (uv);
315 error_model.SetUnit (RateErrorModel::ERROR_UNIT_PACKET);
316 error_model.SetRate (error_p);
317
318 PointToPointHelper UnReLink;
319 UnReLink.SetDeviceAttribute ("DataRate", StringValue (bandwidth));
320 UnReLink.SetChannelAttribute ("Delay", StringValue (delay));
321 UnReLink.SetDeviceAttribute ("ReceiveErrorModel", PointerValue (&error_model));
322
323
324 InternetStackHelper stack;
325 stack.InstallAll ();
326
327 TrafficControlHelper tchPfifo;
328 tchPfifo.SetRootQueueDisc ("ns3::PfifoFastQueueDisc");
329
330 TrafficControlHelper tchCoDel;
331 tchCoDel.SetRootQueueDisc ("ns3::CoDelQueueDisc");
332
333 Ipv4AddressHelper address;
334 address.SetBase ("10.0.0.0", "255.255.255.0");
335
336 // Configure the sources and sinks net devices
337 // and the channels between the sources/sinks and the gateways
338 PointToPointHelper LocalLink;
339 LocalLink.SetDeviceAttribute ("DataRate", StringValue (access_bandwidth));
340 LocalLink.SetChannelAttribute ("Delay", StringValue (access_delay));
341
342 Ipv4InterfaceContainer sink_interfaces;
343
344 DataRate access_b (access_bandwidth);
345 DataRate bottle_b (bandwidth);
346 Time access_d (access_delay);
347 Time bottle_d (delay);
348
349 uint32_t size = static_cast<uint32_t>((std::min (access_b, bottle_b).GetBitRate () / 8) *
350 ((access_d + bottle_d) * 2).GetSeconds ());
351
352 Config::SetDefault ("ns3::PfifoFastQueueDisc::MaxSize",
353 QueueSizeValue (QueueSize (QueueSizeUnit::PACKETS, size / mtu_bytes)));
354 Config::SetDefault ("ns3::CoDelQueueDisc::MaxSize",
355 QueueSizeValue (QueueSize (QueueSizeUnit::BYTES, size)));
356
357 for (uint32_t i = 0; i < num_flows; i++)
358 {
359 NetDeviceContainer devices;
360 devices = LocalLink.Install (sources.Get (i), gateways.Get (0));
361 tchPfifo.Install (devices);
362 address.NewNetwork ();
363 Ipv4InterfaceContainer interfaces = address.Assign (devices);
364
365 devices = UnReLink.Install (gateways.Get (0), sinks.Get (i));
366 if (queue_disc_type.compare ("ns3::PfifoFastQueueDisc") == 0)
367 {
368 tchPfifo.Install (devices);
369 }
370 else if (queue_disc_type.compare ("ns3::CoDelQueueDisc") == 0)
371 {
372 tchCoDel.Install (devices);
373 }
374 else
375 {
376 NS_FATAL_ERROR ("Queue not recognized. Allowed values are ns3::CoDelQueueDisc or ns3::PfifoFastQueueDisc");
377 }
378 address.NewNetwork ();
379 interfaces = address.Assign (devices);
380 sink_interfaces.Add (interfaces.Get (1));
381 }
382
383 NS_LOG_INFO ("Initialize Global Routing.");
384 Ipv4GlobalRoutingHelper::PopulateRoutingTables ();
385
386 uint16_t port = 50000;
387 Address sinkLocalAddress (InetSocketAddress (Ipv4Address::GetAny (), port));
388 PacketSinkHelper sinkHelper ("ns3::TcpSocketFactory", sinkLocalAddress);
389
390 for (uint16_t i = 0; i < sources.GetN (); i++)
391 {
392 AddressValue remoteAddress (InetSocketAddress (sink_interfaces.GetAddress (i, 0), port));
393 Config::SetDefault ("ns3::TcpSocket::SegmentSize", UintegerValue (tcp_adu_size));
394 BulkSendHelper ftp ("ns3::TcpSocketFactory", Address ());
395 ftp.SetAttribute ("Remote", remoteAddress);
396 ftp.SetAttribute ("SendSize", UintegerValue (tcp_adu_size));
397 ftp.SetAttribute ("MaxBytes", UintegerValue (data_mbytes * 1000000));
398
399 ApplicationContainer sourceApp = ftp.Install (sources.Get (i));
400 sourceApp.Start (Seconds (start_time * i));
401 sourceApp.Stop (Seconds (stop_time - 3));
402
403 sinkHelper.SetAttribute ("Protocol", TypeIdValue (TcpSocketFactory::GetTypeId ()));
404 ApplicationContainer sinkApp = sinkHelper.Install (sinks.Get (i));
405 sinkApp.Start (Seconds (start_time * i));
406 sinkApp.Stop (Seconds (stop_time));
407 }
408
409 // Set up tracing if enabled
410 if (tracing)
411 {
412 std::ofstream ascii;
413 Ptr<OutputStreamWrapper> ascii_wrap;
414 ascii.open ((prefix_file_name + "-ascii").c_str ());
415 ascii_wrap = new OutputStreamWrapper ((prefix_file_name + "-ascii").c_str (),
416 std::ios::out);
417 stack.EnableAsciiIpv4All (ascii_wrap);
418
419 Simulator::Schedule (Seconds (0.00001), &TraceCwnd, prefix_file_name + "-cwnd.data");
420 Simulator::Schedule (Seconds (0.00001), &TraceSsThresh, prefix_file_name + "-ssth.data");
421 Simulator::Schedule (Seconds (0.00001), &TraceRtt, prefix_file_name + "-rtt.data");
422 Simulator::Schedule (Seconds (0.00001), &TraceRto, prefix_file_name + "-rto.data");
423 Simulator::Schedule (Seconds (0.00001), &TraceNextTx, prefix_file_name + "-next-tx.data");
424 Simulator::Schedule (Seconds (0.00001), &TraceInFlight, prefix_file_name + "-inflight.data");
425 Simulator::Schedule (Seconds (0.1), &TraceNextRx, prefix_file_name + "-next-rx.data");
426 }
427
428 if (pcap)
429 {
430 UnReLink.EnablePcapAll (prefix_file_name, true);
431 LocalLink.EnablePcapAll (prefix_file_name, true);
432 }
433
434 // Flow monitor
435 FlowMonitorHelper flowHelper;
436 if (flow_monitor)
437 {
438 flowHelper.InstallAll ();
439 }
440
441 Simulator::Stop (Seconds (stop_time));
442 Simulator::Run ();
443
444 if (flow_monitor)
445 {
446 flowHelper.SerializeToXmlFile (prefix_file_name + ".flowmonitor", true, true);
447 }
448
449 Simulator::Destroy ();
450 return 0;
451 }
452